In my main activity, I have an actionbar with NAVIGATION_MODE_TABS
. The contents of each tab is a listfragment.
I'd like to make it so that that when a listitem is clicked, a new fragment is brought into view, and the actionbar mode is changed to NAVIGATION_MODE_STANDARD
(so that the tabs are now hidden).
I've managed to get this to work with the following code:
In the listitemclick method:
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Fragment albumListFragment = new AlbumListFragment();
albumListFragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, albumListFragment);
ft.addToBackStack(null);
// Commit the transaction
ft.commit();
Log.i("FragmentList", "Item clicked: " + id);
The problem is when I press the back button, the tabs are still gone, and the previous fragment doesn't come back into view.
Am I doing something wrong.. Is it something to do with the fragment backstack? Should I go about it another way, or even override the backpress?
--Edit--
For clarity - I am calling addToBackStack when I call fragmenttransaction.replace, but when I press the back button, the previous fragment is not restored.
addToBackStack. Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.
At runtime, a FragmentManager can add, remove, replace, and perform other actions with fragments in response to user interaction. Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class.
Maybe this is a little late, I tried to re-construct your problem for hours but didnot succeed. I tried to switch from a listview in a fragment hosted by a ActionBar Tab to another view by the way described in your question(i.e. ft.replace(android.R.id.content, albumListFragment);) but with no effect. After some google and stackoverflow, I use this way to successfully switch the fragment within the tab, yet with no problem as you have.
As the "action bar is not showing correctly" problem, its probably caused by the backstack revert and UI change as describe in android developer guide. you might try to override the onBackStackChanged() callback and re-construct the UI.
As the "backstack does not bring the previous fragment back" problem, you may want to post a little more code to help others re-construct the problem to find the answer.
Wish helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With