1

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

fragments

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

dracula
  • 4,413
  • 6
  • 26
  • 31
  • When your user clicks a news on the fragment 1, does it reach the `if` or the `else` statement? Also where does it reach when you are on the `DetailFragment` and you press the back button, the `if`, the `else if` or the `else`? Please explain better what you want to accomplish. What do you want to happen when you press the back button? – rogcg May 21 '14 at 11:31
  • 1
    hi rogcg , edited my question. The only thing i want is showing detail fragment without losing main fragments state and data. so if user press back fragment 1 won't be reload its data and won't loss scroll offset – dracula May 21 '14 at 11:43
  • try using the `add` method instead of `replace` method. and I don't think you need to override the `onBackPressed()` method. let me know if it works and I'll post as the correct answer for you to mark it. – rogcg May 21 '14 at 12:00
  • it doesn't work. it will always add new fragment over old one. and old one will be visible. and i think this is not good for memory – dracula May 21 '14 at 12:24
  • another thing you could try is, in your `onBackPressed()` check to see if the `fragment.isAdded()` is true, and if so, rather than doing a `transaction.replace()` just do a `transaction.show()`. This keeps the fragment from being recreated if it's already on the stack - no state saving needed. Something like [this answer](http://stackoverflow.com/a/13088867/399459). Let me know if it works. – rogcg May 21 '14 at 12:39

0 Answers0