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 
     22 import com.example.android.apis.R;
     23 
     24 import android.app.Activity;
     25 import android.content.SharedPreferences;
     26 import android.os.Bundle;
     27 import android.widget.EditText;
     28 import android.widget.TextView;
     29 
     30 /**
     31  * Simple example of using persistent preferences to retain a screen's state.
     32  * <p>This can be used as an alternative to the normal
     33  * <code>onSaveInstanceState()</code> mechanism, if you
     34  * wish the state to persist even after an activity is finished.</p>
     35  *
     36  * <p>Note that using this approach requires more care, since you are sharing
     37  * the persistent state potentially across multiple instances of the activity.
     38  * In particular, if you allow a new instance of the activity to be launched
     39  * directly on top of the existing instance, the state can get out of sync
     40  * because the new instance is resumed before the old one is paused.</p>
     41  *
     42  * <p>For any persistent state that is not simplistic, a content
     43  * provider is often a better choice.</p>
     44  *
     45  * <p>In this example we are currently saving and restoring the state of the
     46  * top text editor, but not of the bottom text editor.  You can see the difference
     47  * by editing the two text fields, then going back from the activity and
     48  * starting it again.</p>
     49  *
     50  * <h4>Demo</h4>
     51  * App/Activity/Save &amp; Restore State
     52  *
     53  * <h4>Source files</h4>
     54  * <table class="LinkTable">
     55  *         <tr>
     56  *             <td class="LinkColumn">src/com.example.android.apis/app/PersistentState.java</td>
     57  *             <td class="DescrColumn">The Save/Restore Screen implementation</td>
     58  *         </tr>
     59  *         <tr>
     60  *             <td class="LinkColumn">/res/any/layout/save_restore_state.xml</td>
     61  *             <td class="DescrColumn">Defines contents of the screen</td>
     62  *         </tr>
     63  * </table>
     64  *
     65  */
     66 public class PersistentState extends Activity
     67 {
     68     /**
     69      * Initialization of the Activity after it is first created.  Here we use
     70      * {@link android.app.Activity#setContentView setContentView()} to set up
     71      * the Activity's content, and retrieve the EditText widget whose state we
     72      * will persistent.
     73      */
     74     @Override
     75     protected void onCreate(Bundle savedInstanceState) {
     76         // Be sure to call the super class.
     77         super.onCreate(savedInstanceState);
     78 
     79         // See assets/res/any/layout/save_restore_state.xml for this
     80         // view layout definition, which is being set here as
     81         // the content of our screen.
     82         setContentView(R.layout.save_restore_state);
     83 
     84         // Set message to be appropriate for this screen.
     85         ((TextView)findViewById(R.id.msg)).setText(R.string.persistent_msg);
     86 
     87         // Retrieve the EditText widget whose state we will save.
     88         mSaved = (EditText)findViewById(R.id.saved);
     89     }
     90 
     91     /**
     92      * Upon being resumed we can retrieve the current state.  This allows us
     93      * to update the state if it was changed at any time while paused.
     94      */
     95     @Override
     96     protected void onResume() {
     97         super.onResume();
     98 
     99         SharedPreferences prefs = getPreferences(0);
    100         String restoredText = prefs.getString("text", null);
    101         if (restoredText != null) {
    102             mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
    103 
    104             int selectionStart = prefs.getInt("selection-start", -1);
    105             int selectionEnd = prefs.getInt("selection-end", -1);
    106             if (selectionStart != -1 && selectionEnd != -1) {
    107                 mSaved.setSelection(selectionStart, selectionEnd);
    108             }
    109         }
    110     }
    111 
    112     /**
    113      * Any time we are paused we need to save away the current state, so it
    114      * will be restored correctly when we are resumed.
    115      */
    116     @Override
    117     protected void onPause() {
    118         super.onPause();
    119 
    120         SharedPreferences.Editor editor = getPreferences(0).edit();
    121         editor.putString("text", mSaved.getText().toString());
    122         editor.putInt("selection-start", mSaved.getSelectionStart());
    123         editor.putInt("selection-end", mSaved.getSelectionEnd());
    124         editor.commit();
    125     }
    126 
    127     private EditText mSaved;
    128 }
    129