从某个时候起,我遇到了碎片和后端堆栈的问题。在我的应用程序中,我有一个活动 - MainActiviy
带抽屉和一些碎片。
我的MainActivity
看起来像这样:
public class MainActivity extends ActionBarActivity {
private static final int HOME_NUM = 0;
private static final int ABOUT_NUM = 1;
private boolean firstFragmentChange = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setHomeButtonEnabled(true);
//Some other initalizations
displayFragment(HOME_NUM);
}
private void displayFragment(int position) {
Fragment fragment = null;
switch (position) {
case HOME_NUM:
fragment = new MainFragment();
break;
case ABOUT_NUM:
fragment = new AboutFragment();
break;
default:
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragment);
if (!firstFragmentChange) {
fragmentTransaction.addToBackStack(null);
}
fragmentTransaction.commit();
firstFragmentChange = true;
}
}
MainFragment
类看起来像这样:
public class MainListFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final int ADD = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list_main, container,
false);
setHasOptionsMenu(true);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_fragment_ab, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
displayFragment(ADD);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void displayFragment(int position) {
Fragment fragment = null;
switch (position) {
case ADD:
fragment = new AddFragment();
break;
default:
break;
}
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragment);
fragmentTransaction.commit();
}
}
我想要的和我想要实现的是在我的背堆上放置第一个(初始)片段,即MainFragment
。但是,即使在addToBackStack()
中添加MainActivity
后,它仍然无法正常工作。现在,当我到达这个片段时,我的背堆上加倍了Add
片段。
也许更好地描述它看起来像这样:
现在:
Start -> MainActivity (displays) MainFragment ->(press button)-> AddFragment
(back pressed)-> AddFragment (still stays) -> exit app
我希望它如何运作:
Start -> MainActivity (displays) MainFragment ->(press button)-> AddFragment
(back pressed)-> MainFragment -> exit app
有人可以告诉我如何实现这个目标吗?
答案 0 :(得分:0)
按下按钮,您总能得到一个新的Fragment实例。更好地标记它们并检查它们是否已经存在。这样,您将始终拥有特定片段的一个实例,并且您的问题将会消失。
此外,如果您想检查片段是否已添加到后台堆栈中,请使用Fragment.getBackStackEntryCount()
。如果它返回0,则后台堆中没有碎片。
像这样的东西,也许你需要修改一下,但你应该明白这个想法:
private void displayFragment(int position) {
Fragment fragment = null;
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
switch (position) {
case HOME_NUM:
fragment = fragmentManager.findFragmentByTag(FIRST_TAG);
if (fragment == null) {
fragment = new MainFragment();
}
fragmentTransaction.replace(R.id.frame_container, fragment, FIRST_TAG); // Set the tag
break;
case ABOUT_NUM:
fragment = fragmentManager.findFragmentByTag(SECOND_TAG);
if (fragment == null) {
fragment = new AboutFragment();
}
fragmentTransaction.replace(R.id.frame_container, fragment, SECOND_TAG); // Set the tag
break;
default:
break;
}
if (fragmentManager.getBackStackEntryCount == 0) {
fragmentTransaction.addToBackStack(null);
}
fragmentTransaction.commit();
}
还要确保使用此方法一次!片段中不需要额外的方法。