Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Using the Application Class</strong></p> <p>Depending on what you're doing in your initialization you could consider creating a new class that extends <code>Application</code> and moving your initialization code into an overridden <code>onCreate</code> method within that class.</p> <pre><code>public class MyApplicationClass extends Application { @Override public void onCreate() { super.onCreate(); // TODO Put your application initialization code here. } } </code></pre> <p>The <code>onCreate</code> in the application class is only called when the entire application is created, so the Activity restarts on orientation or keyboard visibility changes won't trigger it.</p> <p>It's good practice to expose the instance of this class as a singleton and exposing the application variables you're initializing using getters and setters.</p> <p><em>NOTE: You'll need to specify the name of your new Application class in the manifest for it to be registered and used:</em></p> <pre><code>&lt;application android:name="com.you.yourapp.MyApplicationClass" </code></pre> <p><strong>Reacting to Configuration Changes</strong> <em>[UPDATE: this is deprecated since API 13; <a href="http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html" rel="noreferrer">see the recommended alternative</a>]</em></p> <p>As a further alternative, you can have your application listen for events that would cause a restart – like orientation and keyboard visibility changes – and handle them within your Activity.</p> <p>Start by adding the <code>android:configChanges</code> node to your Activity's manifest node</p> <pre><code>android:configChanges="keyboardHidden|orientation" </code></pre> <p>or for <a href="https://stackoverflow.com/a/9550231/2291">Android 3.2 (API level 13) and newer</a>:</p> <pre><code>android:configChanges="keyboardHidden|orientation|screenSize" </code></pre> <p>Then within the Activity override the <code>onConfigurationChanged</code> method and call <code>setContentView</code> to force the GUI layout to be re-done in the new orientation.</p> <pre><code>@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(R.layout.myLayout); } </code></pre>
    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.
    3. 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