当我单击布局中的后退图标时,它会转到上一个 fragment ,但不会转到它被杀死的 fragment 。解决方案是什么?
我正在使用 finish()
和 backstack 但它对我不起作用
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android.support.v4.app.Fragment onlineFragments = new OnlineFragments();
android.support.v4.app.FragmentManager fragmentManagerprofile = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentprofileTransaction = fragmentManagerprofile.beginTransaction();
fragmentprofileTransaction.replace(R.id.background_fragment, onlineFragments);
fragmentprofileTransaction.commit();
}
});
fragment A
case R.id.recharge:
HomeActvity.toolbar.setVisibility(View.GONE);
android.support.v4.app.Fragment Recharge = new Prepaid_recharge();
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, Recharge);
fragmentTransaction.commit();
break;
最佳答案
每当你在 fragment 之间进行事务并且你想导航回到前一个 fragment (后退按钮)时,在事务中,你必须在提交之前将此事务添加到 backStack:
Android 文档:
“然而,在您调用 commit() 之前,您可能想要调用 addToBackStack(),以便将事务添加到 fragment 事务的后退栈中。此返回堆栈由 Activity 管理,允许用户通过按“返回”按钮返回到之前的 fragment 状态。”
https://developer.android.com/guide/components/fragments.html#Transactions
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
关于android - android 中的 Fragments 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38956569/