I have fragments in stack like Main frag --> A frag --> B frag --> C frag --> D frag.
How can I go back to main fragment from any of A, B, C, D fragment. For eg: if i clicked button in D, I should be back to Main fragment. Same should happen in case of button click on fragment C. And, on back button click from Main fragment, it should not go back to Fragment D or C.
I tried to pop out all fragment from stack & start again from main fragment. But it doesn't work.
private void onButtonClick(){
MainFragment mainFrag= new MainFragment();
getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);//pop back stack inclusive pops all fragment
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.mainFragment, mainFragment).addToBackStack(null).commit();
}
Edit : Ok, it looks like above code is going back to main fragment. But after showing Main fragment for a second, the app minimizes. After main fragment minimizes, following log is shown on Android logcat.
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 570
V/FA: Activity paused, time: 14995077
V/FA: Inactivity, disconnecting from the service