I am learning how to use fragments. I have three instances of Fragment
that are initialized at the top of the class. I am adding the fragment to an activity like this:
Declaring and initializing:
Fragment A = new AFragment();
Fragment B = new BFragment();
Fragment C = new CFragment();
Replacing/Adding:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, A);
ft.addToBackStack(null);
ft.commit();
These snippets are working properly. Every fragment is attached to the activity, and is saved to the back stack without any problem.
So when I launch A
, C
, and then B
, the stack looks like this:
| |
|B|
|C|
|A|
___
And when I press the 'back' button, B
is destroyed and C
is resumed.
But, when I launch fragment A
a second time, instead of resuming from back stack, it is added at the top of the back stack
| |
|A|
|C|
|A|
___
But I want to resume A
and destroy all fragments on top of it (if any). Actually, I just like the default back stack behavior.
How do I accomplish this?
Expected: (A
should be resumed and top fragments should be destroyed)
| |
| |
| |
|A|
___
Edit: (suggested by A--C)
This is my trying code:
private void selectItem(int position) {
Fragment problemSearch = null, problemStatistics = null;
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
String backStateName = null;
Fragment fragmentName = null;
boolean fragmentPopped = false;
switch (position) {
case 0:
fragmentName = profile;
break;
case 1:
fragmentName = submissionStatistics;
break;
case 2:
fragmentName = solvedProblemLevel;
break;
case 3:
fragmentName = latestSubmissions;
break;
case 4:
fragmentName = CPExercise;
break;
case 5:
Bundle bundle = new Bundle();
bundle.putInt("problem_no", problemNo);
problemSearch = new ProblemWebView();
problemSearch.setArguments(bundle);
fragmentName = problemSearch;
break;
case 6:
fragmentName = rankList;
break;
case 7:
fragmentName = liveSubmissions;
break;
case 8:
Bundle bundles = new Bundle();
bundles.putInt("problem_no", problemNo);
problemStatistics = new ProblemStatistics();
problemStatistics.setArguments(bundles);
fragmentName = problemStatistics;
default:
break;
}
backStateName = fragmentName.getClass().getName();
fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped) {
ft.replace(R.id.content_frame, fragmentName);
}
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(backStateName);
ft.commit();
// I am using drawer layout
mDrawerList.setItemChecked(position, true);
setTitle(title[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
The problem is, when I launch A
and then B
, then press 'back', B
is removed and A
is resumed. and pressing 'back' a second time should exit the app. But it is showing a blank window and I have to press back a third time to close it.
Also, when I launch A
, then B
, then C
, then B
again...
Expected:
| |
| |
|B|
|A|
___
Actual:
| |
|B|
|B|
|A|
___
Should I override onBackPressed()
with any customization or am I missing something?