NeoWang
NeoWang

Reputation: 18513

Why doesn't my SplashActivity's initialization code get executed?

My app has some global/static data structures that need to be initialized before showing the main Activity, so I put the work into onCreate method of my SplashActivity, which just shows a splash image for 2 seconds, starts another activity, and finishes itself:

initializeGlobalData(); 

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(i);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            finish();
        }
    }, 2000);

Now, my app sometimes mysteriously crashes because of null pointer reference - some global data structures are not initialized. It could only mean that the onCreate method of SplashActivity is not called (right?).

I have no idea how to reproduce this, but it happens quite often. It's possible I left the app in the background, and re-enter. But application level data should not be released, right?

Upvotes: 0

Views: 36

Answers (3)

Deˣ
Deˣ

Reputation: 4371

Why not just initialize them in Application class

public class MyApplication extends Application {

    private int globalData = 0;

    public int getGlobalData() {
        return globalData;
    }

    public void setGlobalData(int globalData) {
        this.globalData = globalData ;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setGlobalData(100)
    }
 }

Change the application tag in manifest file-

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".MyApplication" .  // declare the application class
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

Now you can access this anywhere in the app like

((MyApplication) getApplicationContext()).getGlobalData()

The behavior you are having is because if the app is in the background even if it has not been closed, Android OS can clear the initialized variable if not being used.

Upvotes: 1

Ben P.
Ben P.

Reputation: 54204

It's possible I left the app in the background, and re-enter. But application level data should not be released, right?

It depends on what you mean when you say "global/static data structures that need to be initialized".

If the user leaves your app, it is expected that the Android OS might terminate your app's process. When this happens, anything that is stored only in memory will be lost.

A common example is e.g. some public static value that you load once and then refer to throughout your application. When the OS terminates your app's process, and then the user returns to your app, that public static value will need to be re-initialized.

Upvotes: 1

Michael Dougan
Michael Dougan

Reputation: 1698

Splash Activities are, by nature, short lived and shouldn't be relied upon for any global data structures. If you need such, you need to create an Application class and do all of your global data structure initialization there. Those will not go out of scope for the lifetime of the app.

Upvotes: 1

Related Questions