The BackStack snippet does not work, although addToBackStack (null) is called

In my activity, I call:

getFragmentManager().beginTransaction().addToBackStack(null) .add(R.id.fragment_container, new UserPlaylistsFragment()) .addToBackStack(null).commit(); 

This snippet is correctly added to the view. The fragment contains a list. When an item is clicked, the following code will be executed:

 getFragmentManager() .beginTransaction() .replace(R.id.fragment_container, new ViewPlaylistFragment(), "ViewPlaylistFragment").addToBackStack(null).commit(); 

This also works. UserPlaylistsFragment is replaced by ViewPlaylistFragment. Strange thing: When you hit the back button, nothing happens. And when you double-click the application closes (no errors).

"BackStack" doesn't seem to work for me. Anyone suggest?

My layout:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout> </LinearLayout> 
+4
source share
2 answers

Remove your first .addToBackStack (null) call in your initial FragmentTransaction.

+2
source

Never call addToBackStack () twise .. this will ruin it

0
source

Source: https://habr.com/ru/post/1395638/


All Articles