Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way is to save data in <code>onPause()</code> and restore it in <code>onResume()</code>. Next question is where to store data. The are few way to do that:</p> <ol> <li><strong>Using custom application context</strong></li> </ol> <p>You can extend <code>android.app.Application</code> and register your class in your manifest file by</p> <pre><code>&lt;application android:name="your.application.class" ... </code></pre> <p>This will allow you to get singleton instance of your class by calling `Context.getApplicationContext().</p> <p>For an example you can create</p> <pre><code>public class MyContext extends Application { Bundle mySavedData = null; public void setSavedData(Bundle data){ mySavedData = data; } public Bundle getSavedData() { return mySavedData; } } </code></pre> <p>and then use it like this</p> <pre><code>@overide public void onResume(){ ... Bundle state = ((MyContext) getApplicationContext()).getSavedData(); if(state != null) { /* restore states */ } ... } @overide public void onPause(){ ... Bundle state = new Bundle(); ... /* save your data here and save it into the context or set null otherwise*/ ((MyContext) getApplicationContext()).setSavedData(state); ... } </code></pre> <ol> <li><strong>Using singleton pattern</strong></li> </ol> <p>Instead of defining context you can create singleton instance</p> <pre><code>public class MySingleton { static MySingleton instance; public static MySingleton getInstance() { if(instance == null){ instance = new MySingleton(); } return instance; } public Bundle mySavedData = null; void setSavedData(Bundle data){ mySavedData = data; } public Bundle getSavedData() { return mySavedData; } } </code></pre> <p>and you can use it </p> <pre><code>@overide public void onResume(){ ... Bundle state = MySingleton.getInstance().getSavedData(); if(state != null) { /* restore states */ } ... } @overide public void onPause(){ ... Bundle state = new Bundle(); ... /* save your data here and save it into the context or set null otherwise*/ MySingleton.getInstance().setSavedData(state); ... } </code></pre> <p>Be aware that context and singletons are destroyed if application is killed. If you want to store your data permanently than using application document folder or database if recommend but I think that isn't what you looking for.</p> <p>I hope this will help you....</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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