Home
› Uncategorized
Fragment.setRetainInstance (boolean retain) control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated:
Example:
In the example, there are two Fragment, MyFragment and MyFragment2. Basically, both Fragments are same, except that MyFragment2 call setRetainInstance(true) in onCreate(). It can be noticed that onCreate() and onDestroy() of MyFragment2 will not be called when orientation change, and the field myState of MyFragment2 will not be cleared.
Main layout, contain two fragments.
/res/layout/fragmentlayout.xml, the layout of the fragment
MyFragment.java
MyFragment2.java, extends MyFragment, override onCreate() to call setRetainInstance(true).
Download the files.
- onDestroy() will not be called (but onDetach() still will be, because the fragment is being detached from its current activity).
- onCreate(Bundle) will not be called since the fragment is not being re-created.
- onAttach(Activity) and onActivityCreated(Bundle) will still be called.
Example:
In the example, there are two Fragment, MyFragment and MyFragment2. Basically, both Fragments are same, except that MyFragment2 call setRetainInstance(true) in onCreate(). It can be noticed that onCreate() and onDestroy() of MyFragment2 will not be called when orientation change, and the field myState of MyFragment2 will not be cleared.
Main layout, contain two fragments.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <fragment class="com.example.androidretaininstance.MyFragment" android:id="@+id/myfragment1" android:layout_width="0px" android:layout_weight="1" android:layout_height="match_parent" /> <fragment class="com.example.androidretaininstance.MyFragment2" android:id="@+id/myfragment2" android:layout_width="0px" android:layout_weight="1" android:layout_height="match_parent" /> </LinearLayout>
/res/layout/fragmentlayout.xml, the layout of the fragment
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/myid" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/mystatus" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
MyFragment.java
package com.example.androidretaininstance; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; public class MyFragment extends Fragment { int myID; String myState = ""; TextView textID; TextView textStatus; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View myFragmentView = inflater.inflate(R.layout.fragmentlayout, container, false); textID = (TextView)myFragmentView.findViewById(R.id.myid); textStatus = (TextView)myFragmentView.findViewById(R.id.mystatus); textID.setText("ID = " + String.valueOf(myID)); updateStatus(myID + ":onCreateView()"); return myFragmentView; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myID = getId(); updateStatus(myID + ":onCreate()"); } @Override public void onDestroy() { super.onDestroy(); updateStatus(myID + ":onDestroy()"); } private void updateStatus(String st){ if(textStatus == null){ myState += st + " (delayed)\n"; }else{ myState += st +"\n"; textStatus.setText(myState); } Toast.makeText(getActivity(), st, Toast.LENGTH_LONG).show(); } }
MyFragment2.java, extends MyFragment, override onCreate() to call setRetainInstance(true).
package com.example.androidretaininstance; import android.os.Bundle; public class MyFragment2 extends MyFragment { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setRetainInstance(true); } }

-- Delivered by Feed43 service
Related Posts
There is no other posts in this category.Popular
-
The Avengers – Iron Man Mark VII 1.1 APKThe Avengers – Iron Man Mark VII 1.1 APK Suit up and Blast off in the First-Ever Fully-Interactive …
-
How To: Copy And Paste In Android Mobile'>The copy, cut, paste and select the features Android has many nuances. In this how-to, we will foc…
-
[Android] The Dark Knight Rises v1.1.1 APK by GameloftThe Dark Knight Rises is one of the hottest games just released by Gameloft in July 2012 on Androi…
-
[Android] Battleloot Adventure HD v1.0.7 (apk + data)In Battleloot Adventure HD , let's roll out and your adventure is about to begin. In the kingdo…
-
Angry Birds Space v1.0.1 for AndroidAngry Birds Space v1.0.1 for Android The #1 mobile game of all time blasts off into space! All new …
-
[Android] Zuma's Revenge HD v1.0.8 Free Download [Mediafire]Zuma’s Revenge Android , similar to Bonsai Blast, is one of the most fascinating puzzle game fr…
-
[PC Game] Naruto Ninja Memories [mediafire]Naruto: Ninja Memories 1.1 (PC/2010) | 4.58 GB Genre: Fighting/Arcade | Developer: NarutoPlanet |…
Post a Comment
Post a Comment