There are several similar answers, but not in this situation.
My situation is simple.
I have an Activity with two different layouts, one in Portrait, the other in Landscape.
In Portrait , I use <FrameLayout>and add Fragmentto it dynamically .
In Landscape, I use <fragment>, therefore, Fragment static . (it doesn't really matter)
First it starts with Portrait , then I added Fragmentsimply:
ListFrag listFrag = new ListFrag();
getSupportFragmentManager().beginTransaction()
.replace(R.id.FragmentContainer, listFrag).commit();
where ListFrag extends ListFragment.
Then I rotate the screen. I found that listFrag is recreated in landscape mode. (In which I noticed that the method is onCreate()again called with a non-zero package)
I tried using setRetainInstance(false), like @NPike, in this post . But getRetainInstance()already falseby default. He does not do what I expected from. Can anyone explain this?
The snippet I'm dealing with is the ListFragmentone that does setListAdapter()at onCreate(). Therefore, the method cannot be used here. (or I don’t know how to apply). if (container == null) return null;
. if (bundle != null) setListAdapter(null); else setListAdapter(new ...); ListFragment? / , /, ? ( if (container == null) return null;)
Edit:
, , getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentById(R.id.FragmentContainer)).commit(); onSaveInstanceState(). .
, , WhatsApp TXT, . ( , ).
, . onCreate(Bundle) onRestoreInstanceState(Bundle). (1), onCreate(Bundle) onRestoreInstanceState(Bundle) , . Activity ( Bundle).
, , , getSupportFragmentManager()...replace(...).commit(); Portrait.
, :)
MainActivity.java
package com.example.fragremovetrial;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.FragmentContainer) != null) {
System.out.println("-- Portrait --");
ListFrag listFrag = new ListFrag();
getSupportFragmentManager().beginTransaction()
.replace(R.id.FragmentContainer, listFrag).commit();
} else {
System.out.println("-- Landscape --");
}
}
@Override
protected void onResume() {
super.onResume();
if (findViewById(R.id.FragmentContainer) != null) {
System.out.println("getRetainInstance = " +
getSupportFragmentManager().findFragmentById(R.id.FragmentContainer).getRetainInstance());
}
}
}
/activity _main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
layout-land/activity_main.xml ( )
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
ListFrag.java
package com.example.fragremovetrial;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.widget.ArrayAdapter;
public class ListFrag extends ListFragment {
private String[] MenuItems = { "Content A", "Contnet B" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("ListFrag.onCreate(): " + (savedInstanceState == null ? null : savedInstanceState));
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, MenuItems));
}
}
,
- -
ListFrag.onCreate(): null
getRetainInstance = false
( → )
ListFrag.onCreate(): Bundle [{android: view_state=android.util.SparseArray@4052dd28}]
- -
id 16908298 , .
( → )
ListFrag.onCreate(): Bundle [{android: view_state=android.util.SparseArray@405166c8}]
- -
ListFrag.onCreate(): null
getRetainInstance = false
( → )
ListFrag.onCreate(): Bundle [{android: view_state=android.util.SparseArray@4050fb40}]
- -
id 16908298 , .
( → )
ListFrag.onCreate(): Bundle [{android: view_state=android.util.SparseArray@40528c60}]
- -
ListFrag.onCreate(): null
getRetainInstance = false
, , . (, )