Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.app;
     18 
     19 // Need the following import to get access to the app resources, since this
     20 // class is in a sub-package.
     21 import com.example.android.apis.R;
     22 
     23 import android.app.Activity;
     24 import android.os.Bundle;
     25 import android.widget.EditText;
     26 import android.widget.TextView;
     27 
     28 /**
     29  * <p>Demonstrates required behavior of saving and restoring dynamic activity
     30  * state, so that an activity will restart with the correct state if it is
     31  * stopped by the system.</p>
     32  *
     33  * <p>In general, any activity that has been paused may be stopped by the system
     34  * at any time if it needs more resources for the currently running activity.
     35  * To handle this, before being paused the
     36  * {@link android.app.Activity#onSaveInstanceState onSaveInstanceState()} method is called before
     37  * an activity is paused, allowing it to supply its current state.  If that
     38  * activity then needs to be stopped, upon restarting it will receive its
     39  * last saved state in
     40  * {@link android.app.Activity#onCreate}.</p>
     41  * <p>In this example we are currently saving and restoring the state of the
     42  * top text editor, but not of the bottom text editor.  You can see the difference
     43  * by editing the two text fields, then going to a couple different
     44  * applications while the demo is running and then returning back to it.  The
     45  * system takes care of saving a view's state as long as an id has been
     46  * assigned to the view, so we assign an ID to the view being saved but not
     47  * one to the view that isn't being saved.</p>
     48  * <h4>Demo</h4>
     49  * App/Activity/Save &amp; Restore State
     50  * <h4>Source files</h4>
     51  * <table class="LinkTable">
     52         <tr>
     53             <td class="LinkColumn">src/com.example.android.apis/app/SaveRestoreState.java</td>
     54             <td class="DescrColumn">The Save/Restore Screen implementation</td>
     55         </tr>
     56         <tr>
     57             <td class="LinkColumn">/res/any/layout/save_restore_state.xml</td>
     58             <td class="DescrColumn">Defines contents of the screen</td>
     59         </tr>
     60 </table>
     61  */
     62 public class SaveRestoreState extends Activity
     63 {
     64     /**
     65      * Initialization of the Activity after it is first created.  Here we use
     66      * {@link android.app.Activity#setContentView setContentView()} to set up
     67      * the Activity's content, and retrieve the EditText widget whose state we
     68      * will save/restore.
     69      */
     70     @Override
     71 	protected void onCreate(Bundle savedInstanceState) {
     72         // Be sure to call the super class.
     73         super.onCreate(savedInstanceState);
     74 
     75         // See assets/res/any/layout/save_restore_state.xml for this
     76         // view layout definition, which is being set here as
     77         // the content of our screen.
     78         setContentView(R.layout.save_restore_state);
     79 
     80         // Set message to be appropriate for this screen.
     81         ((TextView)findViewById(R.id.msg)).setText(R.string.save_restore_msg);
     82     }
     83 
     84     /**
     85      * Retrieve the text that is currently in the "saved" editor.
     86      */
     87     CharSequence getSavedText() {
     88         return ((EditText)findViewById(R.id.saved)).getText();
     89     }
     90 
     91     /**
     92      * Change the text that is currently in the "saved" editor.
     93      */
     94     void setSavedText(CharSequence text) {
     95         ((EditText)findViewById(R.id.saved)).setText(text);
     96     }
     97 }
     98 
     99