Fragmented transactions, action bar and still image

In my main activity, I have an action bar with NAVIGATION_MODE_TABS . The contents of each tab is a piece of the list.

I would like to make a new fragment appear when I click on a list item and the action bar mode is changed to NAVIGATION_MODE_STANDARD (so the tabs are now hidden).

I 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 click the back button, the tabs are still missing and the previous snippet is not coming back.

I'm doing something wrong. Does this have anything to do with the backstack fragment? Do I have to go this route in another way or even cancel the feed?

- Edit -

For clarity, I call addToBackStack when I call the fragmentation function. replace, but when I click the back button, the previous fragment is not restored.

+7
source share
4 answers

Perhaps this is a bit late, I tried several times to rebuild your problem, but it failed. I tried to switch from the list in the fragment placed by the ActionBar tab to another view, as described in your question (i.e. Ft.replace (android.R.id.content, albumListFragment);), but without effect. After some google and stackoverflow, I use this way to successfully switch the fragment on the tab, but without problems like yours.

Since the problem "does not display correctly" of the problem , it is probably caused by the backstack return and user interface change, as described in the Android Developer's Guide . you can try to override the onBackStackChanged () callback and redesign the user interface.

Since the "backstack does not return the previous fragment back" problem , you can post a bit more code to help others redesign the problem to find the answer.

Wish.

+1
source

I experienced the exact same problem and realized that the problem only occurs when setting the ActionBar back to NAVIGATION_MODE_STANDARD.

The only solution for me was to put a new fragment without tabs in the auxiliary activity, which has NAVIGATION_MODE_STANDARD. Then you can easily use the back button functionality provided by Android to return to activity using NAVIGATION_MODE_TABS and save its state.

0
source

FragmentTransaction.replace () actually deletes the previous fragment in the layout and adds a new instance of the fragment.

Try using FragmentTransaction # show () and FragmentTransaction # hide () instead of replacing.

0
source

The back part completes the current activity and moves to the previous action, and not to the previous fragment. (See Hindfoot Guide: http://developer.android.com/guide/components/tasks-and-back-stack.html ).

You could force, as you said, the behavior to override onBackPressed, however, I don’t know that I would change the natural behavior of Android in this way. If you look at other applications, in particular the main applications that do similar things, the return button does not go to the previous fragment, but refers to the previous action.

You also have full control over the behavior of the up button (this is the icon of your application in the action bar with the left arrow). You need to programmatically configure it to use as a button, as described in the ActionBar manual (let me know if you need to know how to do this), and with that you could configure the previous fragment on the stack that you are supported somewhere.

-one
source

All Articles