Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>The first part of this answer is really old -- see below for the <em>right</em> way to do it</strong></p> <p>You can use the Application object to store application persistent objects. <a href="http://developer.android.com/guide/appendix/faq/framework.html#3" rel="nofollow noreferrer">This Android FAQ</a> talks about this problem as well.</p> <p>Something like this:</p> <pre><code>public class MyApplication extends Application{ private String thing = null; public String getThing(){ return thing; } public void setThing( String thing ){ this.thing = thing; } } public class MyActivity extends Activity { private MyApplication app; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = ((MyApplication)getApplication()); String thing = app.getThing(); } } </code></pre> <p><strong>The <em>right</em> way</strong>:</p> <p>When this answer was first written, the documentation for the Activity lifecycle was not as good as it is now. Reading <a href="http://developer.android.com/guide/components/activities.html#SavingActivityState" rel="nofollow noreferrer">Saving Activity State</a> section on the Activity document helps us understand how Android wants us to save state. Essentially, there are two circumstances under which your activity starts: (1) as a new activity and (2) because of a configuration change or when it's recreated after being destroyed due to memory pressure. When your activity starts because it's a new activity, then saveInstanceState is null. It's not null otherwise. If it's null, then your activity should initialize itself from scratch. Fragments are very similar to Activities, and I covered this concept in detail for my <a href="https://docs.google.com/presentation/d/1iqReU8gTnL7wD8xUPAzsOwYYAXsHHS-OQW9bmxOh1Wo/pub?start=false&amp;loop=false&amp;delayms=3000" rel="nofollow noreferrer">AnDevCon-14 slide deck</a>. You can also take a look at the <a href="https://github.com/johnnylambada/andevcon-2014-jl" rel="nofollow noreferrer">sample code</a> for my AnDevCon-14 presentation for more details.</p> <p>Reworking my previous example would look something like the code below. I do change the semantics a bit -- in this second version I assume the string <code>thing</code> is specific to the activity within a specific <a href="http://developer.android.com/guide/components/tasks-and-back-stack.html" rel="nofollow noreferrer">android task</a>, in the previous example it's ambiguous. If you do want to keep the same data around for multiple android tasks, then using either the Application object or another singleton is still your best bet.</p> <pre><code>public class MyActivity extends Activity { private static final String THING = "THING"; private String thing; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState==null) { // First time here (since we last backed out at least) thing = initializeThing(); // somehow we init it } else { // Rehydrate this new instance of the Activity thing = savedInstanceState.getString(THING); } String thing = app.getThing(); } protected void onSaveInstanceState(Bundle outState) { outState.putString(THING, thing); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload