根据我在这里发现的一些帖子,我已经正确实现了BackStackNavigation,但每当我按任意片段上的Back按钮时,应用弹出当前片段,显示前一片段的闪光然后退出。我无法解决这个问题。任何帮助都感激不尽。我做错了还是还有其他的事情?
我的应用程序的结构是,
MainActivity -> NavigationDrawerFragment -> 1. RecyclerViewFragment -> TextViewFragment
2. SlidingTabStrips.ViewPager (Fragment)
3. Some Code (No fragments)
这是onBackPressed
中重写的MainActivity
:
@Override
public void onBackPressed() {
super.onBackPressed();
getFragmentManager().popBackStack(backStack[TOP],
FragmentManager.POP_BACK_STACK_INCLUSIVE);
backStack[TOP] = null;
TOP--;
Log.e("BACK_STACK_COUNT_ON_POP",Integer
.toString(getFragmentManager().getBackStackEntryCount()));
Log.e("TOP",Integer.toString(TOP));
}
现在我用NavigationDrawer
这样替换片段:
switch(position) {
case 0: //Events
fragmentManager.beginTransaction().replace(R.id.container,
EventsRecyclerViewFragment.newInstance(position + 1))
.addToBackStack(EVENT_RV_FRAGMENT).commit();
++TOP;
backStack[TOP] = EVENT_RV_FRAGMENT;
break;
case 1: //User
fragmentManager.beginTransaction().replace(R.id.container,
UserFragment.newInstance(position + 1))
.addToBackStack(USER_FRAGMENT).commit();
++TOP;
backStack[TOP] = USER_FRAGMENT;
break;
case 2: //Logout
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(prefs.getBoolean(KEY_LOGIN, false)) {
prefs.edit().remove(KEY_LOGIN).apply();
prefs.edit().remove(KEY_USERNAME).apply();
prefs.edit().remove(KEY_PASSWORD).apply();
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
Customs.mToast(getBaseContext(), "Logged-out");
}
break;
}
这就是我在RecyclerViewFragment中调用TextViewFragment的方法:
getFragmentManager().beginTransaction()
.replace(R.id.container, eventFragment)
.addToBackStack(EVENT_FRAGMENT)
.commit();
++TOP;
backStack[TOP] = EVENT_FRAGMENT;
NavigationDrawer已加载onCreate
:
点击上面的活动时打开了RecyclerViewFragment:
当我点击上面显示的其中一张卡片时,TextViewFragment已打开:
答案 0 :(得分:0)
一个非常简短的工作示例...我没有检查它是否是一个API问题,它在lvl 21上的工作方式如下。
使用addToBackstack = false添加第一个片段,使用addToBackstack = true添加另一个片段。
活动(xml只包含具有id rlRoot的relativelayout)
public class FragActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_activity);
addFragment(new Frag1(), false, Frag1.TAG);
addFragment(new Frag2(), true, Frag2.TAG);
}
private void addFragment(Fragment fragment, boolean addToBackStack, String tag) {
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.rlRoot, fragment, tag);
if(addToBackStack) trans.addToBackStack(tag);
trans.commit();
}
}
片段
public class Frag1 extends Fragment {
public static final String TAG = Frag1.class.getSimpleName();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView tv = new TextView(getActivity());
tv.setText(String.valueOf(1));
return tv;
}
}
public class Frag2 extends Fragment {
public static final String TAG = Frag1.class.getSimpleName();
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView tv = new TextView(getActivity());
tv.setText(String.valueOf(2));
return tv;
}
}
答案 1 :(得分:0)
好的,我找到了解决方案。
onBackPressed()
中ActionBarActivity.class
的原始实现是..
public void onBackPressed() { /* compiled code */ }
因此,当我们要从堆栈中移除顶部框架时,我们必须在getFragmentManager().popBackStack()
中调用overridden onBackPressed()
。
当堆栈为空时,请拨打super.onBackPressed()
,但没有任何东西可以让应用程序退出。
重写的onBackPressed()应该是这样的:
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
}
else {
super.onBackPressed();
}
}