I have problems in understanding the Up Navigation in the Master Detail Template in Android. In single pane mode (for smartphones) I use a ItemListActivity and a ItemListFragment. If a item in the ListFragment is clicked the ItemDetailFragment is called. Now if I am in this ItemDetailFragment I want to get back to the ItemListFragment by a click on the Up Navigation in the ActionBar.
I understand it like this: I just have to replace my ItemDetailFragment with the ItemListFragment, or?
In my ItemDetailFragment I use this code:
//OnClick auf ActionBar
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
FragmentTransaction trans = getFragmentManager().beginTransaction();
trans.replace(R.id.fragment_container, new ItemListFragment());
trans.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
But the ItemListFragment is then not in the state before the DetailFragment was called. I want to have the same list as before.
If I click the back button the correct action is done, so should I just implement a back click in the up navigation?
As far as i understood with your description above..
1)If you click on.. in your Listfragment item then you are moving to ItemdetailFragment so for that in your ListFragment you need to add BackStack operation. i.e., as follows
transaction.addToBackStack(String);//to identify you can give any name
transaction.commit(); //then commit the transaction
2)When you want to move from your ItemdetailFragment to ListFragment you are using back button in that case in the backbutton you need to call
popBackStack("towhich screen you want to move",0) //for more information
referthis