I have four main fragments and one minor fragment in my app. Let's say Fragment A, B, C, D. There's a ListView at Fragment A. If user click list item, another Fragment (sub A) will be shown up. So.., if user click A -> B -> C -> D. The backstack will be D -> C-> B-> A. This is simple and easy. Here is my problem.., when user clicked a list item at Fragment A, The transition will be A -> sub A -> B -> C -> D. But I don't want sub A on backstack. My desired result is D -> C -> B -> A. I'm currently trying to skip the sub A but still couldn't find a suitable answer.
Asked
Active
Viewed 935 times
2
-
Possible duplicate of [Clear back stack using fragments](http://stackoverflow.com/questions/6186433/clear-back-stack-using-fragments) – Victor Viola Feb 19 '16 at 11:44
-
Possible duplicate of [Removing a Fragment from the back stack](http://stackoverflow.com/questions/9033019/removing-a-fragment-from-the-back-stack) – Robin Vinzenz Feb 19 '16 at 11:44
1 Answers
2
Use this by overriding or any specific back button Click
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();
Override back button like this
// 2.0 and above
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Nuke
- 396
- 2
- 13