Ashish Rathee
Ashish Rathee

Reputation: 449

Why UI state getting retained even activity getting destoyed & created again

Why UI state getting retained even activity getting destoyed & created again:

My activity has a EditText and if i write some text in it eg. ABC and when i rotate it onDestroy getting called and then onCreate() so Edittext should be Empty but its still has the old text.

My Activity:

public class MainActivity extends ActionBarActivity {


    protected void onCreate(Bundle savedInstanceState) {
        debugLog("(++) Create called");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        debugLog("(++) onResume called");
        super.onResume();
    }

    @Override
    protected void onPause() {
        debugLog("(++) onPause called");
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        debugLog("(++) onDestroy called");
        super.onDestroy();
    }

    private void debugLog(String str) {
        Log.d("SAMPLE APP",str);
    }
}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}"
    android:orientation="vertical"
    android:padding="15dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name: "
        android:singleLine="true"
        android:textSize="16sp"
        android:textColor="#000000" />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hobbies:"
        android:singleLine="true"
        android:textSize="16sp"
        android:textColor="#000000" 
        android:layout_marginTop="10dp"/>

    <CheckBox  
        android:id="@+id/checkBox1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:text="Table Tennis" 
        android:layout_marginTop="10dp"/>  

</LinearLayout>

Logcat on Orientation change:

(++) onDestroy called
(++) onPause called
(++) onCreate called
(++) onResume called

Upvotes: 0

Views: 37

Answers (2)

abhishesh
abhishesh

Reputation: 3316

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.

Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate() methods.

Upvotes: 3

Dawars
Dawars

Reputation: 57

Android is saving the content of some of the default Widgets like EditText as long as it has an ID.

Upvotes: 2

Related Questions