Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application getting purged by Android

I start my Android app, which in turns initializes some state on the first screen. The app has a many screens, and after randomly navigating through some screens, I minimize the app using the "Home key". Now after running some other apps from the phone, the OS decides that it needs to free up my app and hence kills it.

Now when I again click on the app icon, the OS remembers the history and tries to go back to the screen from where I minimized the app. But, the problem is since the OS purged my app sometime back, all my states are lost and the screen may not have any relevance.

How do I tackle this? How do I ensure that the OS calls the launcher screen, if it has been purged before and not the Activity in the history?

like image 345
Codevalley Avatar asked Jan 28 '26 03:01

Codevalley


1 Answers

From your above comment about singleton class being initialized I also faced situations similar to you. Since I could not avoid it, what I did was I used the Application class. Whenever the OS decides to purge your app, the next time you launch the app, onCreate on the Application class will be called. Override the onCreate method to initialize the singleton class rather than doing the same in the launcher screen

The code snippet is as follows

public class CellApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Do your singleton class initialization here
    }
}
like image 57
pankajagarwal Avatar answered Jan 29 '26 16:01

pankajagarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!