Android snippets overlapping after screen rotation

This application is a training exercise for me using, among other things, fragments. He has one action that controls some of the various fragments. The first is ListFragment (A), which shows when the application starts and loads the list items from the cursor. Clicking on a list item notifies the action to replace the ListFragment with DetailFragment (B) for that item. If while viewing (B) I rotate the screen, I see (B) on top (A), for example:

http://postimage.org/image/uhy016ds7/

My best guess: this is due to the fact that Android destroys and recreates the activity when the configuration changes. I am wondering what is the purest solution to this problem. Some code snippets follow, let me know if there is anything else I should post.

Res / layout / home.xml

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="fill_parent" android:layout_height="fill_parent" > <fragment android:id="@+id/fragment_place_list" android:name="com.simbiosys.apps.foodfast.ui.PlaceListFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" > </fragment> </FrameLayout> 

MyActivity.onCreate

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home); placeListFragment = (PlaceListFragment) getSupportFragmentManager().findFragmentById( R.id.fragment_place_list); } 

MyActivity.showDetailPane

 public void showDetailPane(String id, String reference) { placeDetailFragment = PlaceDetailFragment.newInstance(id, reference); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction(); ft.addToBackStack(null); ft.hide(placeListFragment); ft.replace(R.id.fragment_container, placeDetailFragment); ft.show(placeDetailFragment); ft.commit(); } 
+6
source share
4 answers

This is because you are using the frame layout as a container. This would not be an overlap for other layouts.

And for what you want to achieve best practice, you need to make an XML file in your layout / folder, where only your list is used. In your layout / you put an XML file with both fragments that are used and located.

Then the system processes the entire creation. fragments. And your activity basically boils down to calling setContentView ().

In a list item, click on it, then use the Fragment Manager to check if a part fragment has been created. If it is created, you call a public function in the detail fragment to set the data.

If it is not created, you create an intent with a new action.

I know two good tutorials: the snippets page on the Android developers page and vogella.de, and you will give a detailed example / tutorial.

+4
source

According to the Android dev page in the Activity onCreate method, you must check if savedInstanceState is null before adding the fragment to the action. The code snippet of the onCreate Activity method from this page states that.

 // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) { return; } 
+5
source

Change FrameLayout to LinerLayout and the problem will be resolved.

This is a FrameLayout related issue.

+3
source

This works for me.

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); .... fragment = getFragmentManager().findFragmentByTag(TAG); if(fragment == null) fragment = new Fragment(); .... } 
+2
source

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


All Articles