简体   繁体   中英

Android Application class not destroyed when last activity is destroyed

When running an app a second time, the Application Class's onCreate is not called. First time around it is. This is reproducible by using Android Studio to create a new app, and then adding a minimal singleton Application class:

package com.whatever.test;
import android.app.Application;
import android.content.Context;
import android.util.Log;

public class MyApp extends Application {
    private static MyApp singleton;

    public static MyApp getInstance(Context context) {
        Log.i("MyApp","---------->getinstance");
        return singleton;
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        singleton = this;
        Log.i("MyApp","---------->act oncreate");
    }
}

and in the activity adding, in onCreate:

MyApp myApp = MyApp.getInstance(this);
Log.i("MainActivity", "-------->onCreate");

and in onDestroy:

@Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.i("MainActivity", "-->onDestroy");
    }

Manifest.xml contains:

 android:name=".MyApp"

I'm pressing the back button on the activity and seeing onDestroy invoked, but this apparently isn't enough to remove the Application class. If I kill the app using the phone's application manager and re-run I'll again see the App onCreate log (but for just one run).

Shouldn't this class be destroyed when the last activity is removed? That's my assumption. Certainly I've read people saying that this class can be destroyed when the app is running so you have to handle reloading any state during the application lifecycle, but nothing reflecting my scenario.

Note: Tested on a physical phone (Galaxy S3)

An application can also have Services, Content Providers, Content Observers, etc. So it's important not to tie the application lifespan to an Activity. The application may be destroyed when it's hidden if the OS needs to free up resources. But in general, you cannot make any assumptions about when that will actually happen.

Saving/reloading state should happen in your Activity onCreate and onDestroy. The only guarantee that you have when your activity is hidden (eg with the back button), then Activity.onStop is called.

I'd recommend reading everything about Activity lifecycle: Activity class documentation

Edit: To directly address your question, "Shouldn't this class (the application) be destroyed when the last activity is removed?". The answer is no. The only lifecycle that you can reliably count on is that of your Activity. And even in that case, Activity.onDestroy isn't as reliable as you're hoping. Pressing the back button to hide the activity will only guarantee that Activity.onStop is called. The Application class doesn't even have an onDestroy method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM