Android cleaned app

I am launching my Android app, which in turn initializes some state on the first screen. The application has many screens, and after accidentally navigating through some screens I will hide the application using the "Home key". Now, after launching some other applications from the phone, the OS decides that he needs to free my application and, therefore, kill it.

Now, when I click the application icon again, the OS remembers the history and tries to return to the screen, from where I minimized the application. But the problem is that the OS somehow canceled my application, all my states are lost, and the screen may not have any significance.

How do i solve this? How to ensure that the OS calls the launch screen if it has been cleared before and not Activity in the history?

+3
source share
2 answers

From the comment above about initializing a singleton class, I also came across situations similar to you. Since I could not avoid this, I used the Application class . Whenever the OS decides to clean your application, the next time you start the application it will be called onCreatein Application class. Override the method onCreateto initialize the singleton class rather than doing the same on the startup screen

Below is a snippet of code

public class CellApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Do your singleton class initialization here
    }
}
+3
source

You will not be sure that the OS brings up the launch screen. The best way to solve this problem is to keep the state of your screens.

, onSaveInstanceState. , . onCreate, . , .

, onCreate null. , , .

+2

Source: https://habr.com/ru/post/1786595/


All Articles