0

Hi Following my code to show detail fragment from list fragment

getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.FragmentContainer1, DealFragment.newInstance(dealItems, position, currentPage, totalCount)).addToBackStack(null).commit();

Now when I press back button I get new ListFragment.ListFragmnt state is not saved.

I referred to some stack questions but haven't got right answer

I tried below code but it causes issues when app goes in background and is killed by system(Like I am opening chrome from my detail view and when I go back from chrome my app is closed and minimised) FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); ft.hide(getActivity().getSupportFragmentManager().findFragmentById(containerId)); ft.add(containerId, detailFragment); ft.addToBackStack(null); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.commit();

Any solution on this problem when I move to ListFragment to detail hot maintain state of detail fragment.

I have referred this link from stack overflow here is the link

I want functionality same as Gmail app when we go from list to detail and come back to list fragment. Scroll position and everything is maintained which is not happening in my case

Community
  • 1
  • 1
amodkanthe
  • 4,119
  • 4
  • 27
  • 55

2 Answers2

0

Try this :

@Override 
public void onBackPressed() { 
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack(); 
    } else { 
        this.finish(); 
    }
}
schinj
  • 686
  • 4
  • 16
0

In your activity set the first fragment:

        ListFragment listFragment = new ListFragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.FragmentContainer1, listFragment );
        transaction.addToBackStack(null);
        transaction.commit();

Then in listfragment call details fragment:

        DetailFragment detailFragment = new DetailFragment();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.FragmentContainer1, detailFragment);
        transaction.addToBackStack(null);
        transaction.commit();
tompadre
  • 789
  • 7
  • 20
  • Yes it is, if you don't override onBackPressed, this will preserve your last known state in previous fragment. Or if you will, nothing would change. – tompadre May 16 '17 at 10:18