gpt4 book ai didi

android - 如何在fragment中实现onBackPressed并到达HomeFragment?

转载 作者:行者123 更新时间:2023-11-30 00:39:03 25 4
gpt4 key购买 nike

我正在使用 3 个 fragment (HomeFragment、RegisterFragment 和 NewsFragment)。当我单击“后退”按钮时,如何从 NewsFragment 移动到直接 HomeFragment。单击后退按钮时,我不想移动到其他 fragment 。

最佳答案

我制作了一个实用程序类来控制我的 fragment 导航。

这是这个类的样子:

public class FragmentController {

private static final String TAG = FragmentController.class.getCanonicalName();
private static FragmentController mInstance;
private ArrayList<BaseFragment> mFragmentsList;
private BaseActivity mActivity;
private FrameLayout mFragmentContainer;

public FragmentController(BaseActivity activity) {
set(activity);
}

public static FragmentController getInstance(BaseActivity activity) {
if (mInstance == null)
mInstance = new FragmentController(activity);
return mInstance;
}

public static void setInstance(FragmentController mInstance) {
FragmentController.mInstance = mInstance;
}

public void set(BaseActivity activity) {
mActivity = activity;
if (mActivity instanceof MainActivity) {
mFragmentContainer = ((MainActivity) mActivity).getFragmentContainer();
}
mFragmentsList = new ArrayList<>();
}

public void addFirstFragment(BaseFragment fragment) {
mFragmentsList.add(fragment);
}

public void presentFragment(BaseFragment fragment, boolean removeAllFromBackstack) {
try {
Log.d(TAG, "presentFragment: " + fragment.getTagFm());
if (mActivity.getCurrentFocus() != null) {
ViewUtils.hideKeyboard(mActivity.getCurrentFocus());
}
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
ft.replace(mFragmentContainer.getId(), fragment, fragment.getTagFm());
ft.addToBackStack(fragment.getTagFm());
ft.commitAllowingStateLoss();
if (mFragmentsList.size() != 0)
if (removeAllFromBackstack) {
if (mFragmentsList.size() == 2) {
mFragmentsList.remove(1);
} else if (mFragmentsList.size() > 2) {
mFragmentsList.subList(1, mFragmentsList.size()).clear();
}
}
if (!isFragmentPresent(fragment))
mFragmentsList.add(fragment);
} catch (Exception e) {
e.printStackTrace();


}
}

public void removeSecondFragment() {
mFragmentsList.remove(1);
}

private boolean isFragmentPresent(BaseFragment fragment) {
for (BaseFragment baseFragment : mFragmentsList) {
if (baseFragment.getTagFm().equals(fragment.getTagFm()))
return true;
}
return false;
}

public BaseFragment getCurrentFragment() {
return mFragmentsList.get(mFragmentsList.size() - 1);
}

public ArrayList<BaseFragment> getFragmentsList() {
return mFragmentsList;
}

现在,我在 Activity 中实例化这个类

mFragmentController = FragmentController.getInstance(this);

现在,我从我的 Activity

执行此 onBackPressed()
@Override
public void onBackPressed() {
if (mFragmentController.getFragmentsList().size() == 1) {
finish();
}
else {
mFragmentController.getFragmentsList().remove(mFragmentController.getFragmentsList().size() - 1);
mFragmentController.presentFragment(mFragmentController.getFragmentsList().get(mFragmentController.getFragmentsList().size() - 1), false);
}
}

FragmentController 类中,您将看到我在BaseFragment 类上使用方法getTagFm()。这基本上是为了识别 que 中存在哪些 fragment ,哪些不存在。 BaseFragment 类将是这样的:

public class BaseFragment extends Fragment {

public String getTagFm() {
if (this.getClass().getSimpleName().equals(LoginFragment.class.getSimpleName())) {
return Constants.TAG_FRAGMENT_LOGIN;
} else if (this.getClass().getSimpleName().equals(OrderFragment.class.getSimpleName())) {
return Constants.TAG_FRAGMENT_ORDER;
} else if (this.getClass().getSimpleName().equals(ProgramFragment.class.getSimpleName())) {
return Constants.TAG_FRAGMENT_PROGRAM;
}
return Constants.TAG_FRAGMENT_ORDER;
}

}

所有这些 fragment (LoginFragmentOrderFragmentProgramFragment)都扩展了 BaseFragment

希望这对某人有帮助。

关于android - 如何在fragment中实现onBackPressed并到达HomeFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42859097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com