I have 3 fragments in my application and a Base Activity which a replace fragments. Fragment 1 is HomeFragment and i have news scroll in it
Fragment 2 is news detail fragment
When user press a news from fragment one , i am replacing fragment 1 with 2 , but when user press back button , fragment 1 is reloading. So i want to prevent this.
I think i can solve this issue by adding fragment 2 over fragment 1 and hide fragment 1. Is there any other way ? The code below works for me if only user pass fragment 2 , but with any other fragments it is messing up.
When user click a news from fragment 1 , isDetailFragment is being true , otherwise false. When user clicks back button , i want to show fragment 1 again and remove fragment 2.
if(isDetailFragment)
{
detailFragment = inputFragment;
ft.hide(mContent)
.add(R.id.content_frame,inputFragment,page)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
}
else
{
mContent = inputFragment;
ft.replace(R.id.content_frame,inputFragment,page)
.addToBackStack(page)
.commit();
}
and on back pressed
@Override
public void onBackPressed()
{
if(detailFragment!=null)
{
getSupportFragmentManager().beginTransaction()
.show(mContent).remove(detailFragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
detailFragment = null;
}
else if(getSupportFragmentManager().getBackStackEntryCount()>1)
getSupportFragmentManager().popBackStack();
else
super.onBackPressed();
}
Is there an alternative way to do that ? Mine doesn't work as expected