I open fragments
like this: [1]+with addToBackStack
) > [2](without addToBackStack
) > [3]
in Fragment 3
when click onBackPress
Button then 2nd
is not visible but 3rd
override 1
.
If I use addToBackStack
in 2nd fragment
also, then everything is fine(No override issue).
But I have to hide
2nd fragment
when user visit3rd fragment
and
direct come back to1st fragment
.
here is my code:
open root fragment
[1] from activity
with fragmentTransaction.add
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
fragmentTransaction.add(R.id.abbcHomeFragmentContainer, new FragmentHomeAbbc(), null);
//fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
[1](with addToBackStack
) >[2] :opening 2nd fragment
FragmentListOfBankAbbc fragment= new FragmentListOfBankAbbc();
fragment.setArguments(bundle);
if (getActivity()!=null){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.abbcHomeFragmentContainer, fragment, null);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
Toast.makeText(getContext(), "Something went wrong (#201)", Toast.LENGTH_SHORT).show();
}
[2](without addToBackStack
) > [3]
private void funOpenFragments(Fragment fragment, Bundle bundle){
fragment.setArguments(bundle);
if (getActivity()!=null){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.abbcHomeFragmentContainer, fragment, null);
//fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
Toast.makeText(getContext(), "Something went wrong (#202)", Toast.LENGTH_SHORT).show();
}
}
Now open 3rd fragment
and click showing 3rd fragment+1st fragment.
2
Answers
When user go [1] > [2] > [3]
Then
onBackPress
user will be [3] > [1].[2] Fragment : here on button click we will open
3rd fragment
.(^_^) Happy Coding
Try this
MainActivity.kt
FirstFragment.kt
SecondFragment.kt
ThirdFragment.kt