Can somebody explain why the onCreate()
and onCreateView()
are being invoked so many times which increments with each orientation change?
Here is very simple app which consists of one Activity
composed of two Fragments
. The second Fragment
loads dynamically. If you define these two Fragments
in main.xml
there would not be such a behavior.
Here is main.xml
:
<fragment class="ets.saeref.Left" android:id="@+id/left_frag" android:layout_weight="70" android:layout_width="match_parent" android:layout_height="match_parent"/> <FrameLayout android:id="@+id/right_frag" android:layout_weight="30" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
Here is left frag:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <Button android:text="Landscape" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
Here is right frag:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff"> <Button android:text="Landscape" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
Left.class:
public class Left extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i("Left", "onCreate()"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i("Left", "onCreateView()"); return inflater.inflate(R.layout.left, container, false); } }
Right.class:
public class Right extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i("Right", "onCreate()"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i("Right", "onCreateView()"); return inflater.inflate(R.layout.right, container, false); } }
Main class:
public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Fragment fg = new Right(); getFragmentManager().beginTransaction().add(R.id.right_frag, fg) .commit(); Log.i("Main", "onCreate()"); } }
Log after several orientation changes:
08-28 21:47:38.220: INFO/Main(1099): onCreate() 08-28 21:47:38.220: INFO/Right(1099): onCreateView() 08-28 21:47:38.220: INFO/Right(1099): onCreateView() 08-28 21:47:38.220: INFO/Right(1099): onCreateView() 08-28 21:47:38.220: INFO/Right(1099): onCreate() 08-28 21:47:38.220: INFO/Right(1099): onCreateView() 08-28 21:47:41.110: INFO/ActivityManager(142): Config changed: {1.0 0mcc0mnc en_US sw800dp w1280dp h752dp xlrg land finger -keyb/v/h -nav/h s.162} 08-28 21:47:41.140: INFO/Right(1099): onCreate() 08-28 21:47:41.140: INFO/Right(1099): onCreate() 08-28 21:47:41.140: INFO/Right(1099): onCreate() 08-28 21:47:41.140: INFO/Right(1099): onCreate() 08-28 21:47:41.170: INFO/Left(1099): onCreate() 08-28 21:47:41.170: INFO/Left(1099): onCreateView() 08-28 21:47:41.170: INFO/Main(1099): onCreate() 08-28 21:47:41.170: INFO/Right(1099): onCreateView() 08-28 21:47:41.170: INFO/Right(1099): onCreateView() 08-28 21:47:41.170: INFO/Right(1099): onCreateView() 08-28 21:47:41.170: INFO/Right(1099): onCreateView() 08-28 21:47:41.190: INFO/Right(1099): onCreate() 08-28 21:47:41.190: INFO/Right(1099): onCreateView() 08-28 21:47:45.070: INFO/ActivityManager(142): Config changed: {1.0 0mcc0mnc en_US sw800dp w800dp h1232dp xlrg port finger -keyb/v/h -nav/h s.163} 08-28 21:47:45.120: INFO/Right(1099): onCreate() 08-28 21:47:45.120: INFO/Right(1099): onCreate() 08-28 21:47:45.120: INFO/Right(1099): onCreate() 08-28 21:47:45.120: INFO/Right(1099): onCreate() 08-28 21:47:45.120: INFO/Right(1099): onCreate() 08-28 21:47:45.130: INFO/Left(1099): onCreate() 08-28 21:47:45.130: INFO/Left(1099): onCreateView() 08-28 21:47:45.130: INFO/Main(1099): onCreate() 08-28 21:47:45.130: INFO/Right(1099): onCreateView() 08-28 21:47:45.130: INFO/Right(1099): onCreateView() 08-28 21:47:45.130: INFO/Right(1099): onCreateView() 08-28 21:47:45.140: INFO/Right(1099): onCreateView() 08-28 21:47:45.140: INFO/Right(1099): onCreateView() 08-28 21:47:45.140: INFO/Right(1099): onCreate() 08-28 21:47:45.140: INFO/Right(1099): onCreateView()
onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.
The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView() . Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.
You might want to read through the documentation on the Activity lifecycle. OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.
onCreate(savedInstanceState); calls the method in the superclass and saved InstanceState of the activity if any thing damage the activity so its saved in instanceState so when reload the activity it will be the same before. Show activity on this post.
I can't point to the documentation which explains this, but the solution is to only create and add the fragment when the activity first loads, like this:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (savedInstanceState == null) { Fragment fg = new Right(); getFragmentManager().beginTransaction().add(R.id.right_frag, fg) .commit(); } Log.i("Main", "onCreate()"); }
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