The onCreate application class is not running when the application is run for the 2nd time

I use the Application class to exchange global variables in actions, and I set them in the onCreate method of the onCreate class. When I run the values ​​of the application variables, they are set to onCreate and when using the application in the actions they change the values ​​of the variables. When I exit the application and start it again, I get the old values, the last values ​​of the variables are set in the actions. This means that onCreate of Application does not start when the application starts again. This is the code in the onCreate method of the Application class.

 @Override public void onCreate() { super.onCreate(); application = this; category = 12; subCategory =22; } 

It looks like the old application object is still in memory, and it does not call onCreate when the application is run the 2nd time.

What needs to be done to make the onCreate of the application class run again or where to initialize the variables in the application class so that the code runs every time.

+4
source share
6 answers

In the Application class, the onCreate () method is called only if the process was completed when the application exited. Usually the process stops when the system needs memory, or if you exit the application using the back button, rather than the home button. However, you cannot rely on this being discontinued.

However, the correct way to pass parameters between actions is intent or preference. In your case, I have the feeling that preference is the way to go.

If you really want to kill your process when you exit the application, you can call System.exit(0); when the user clicks the back button in the first action. This is definitely not recommended, as it means struggling with how the Android OS works and can cause problems.

Read more about it here: Refused the application that was refused?

+1
source

please declare the class name of your application in the manifest file. as below

 <application android:name="com.tt.app.TTApplication" android:label="@string/app_name" 
+5
source

Your application instance is probably still in memory.

Recheck lifecycle methods and verify that the application exits correctly.

Also check if any of your actions are leaking.

0
source

I had the same problem with my application when the onCreate () method of the Application class only started the first time my application loaded. Daniel's decision to use System.exit (0) did the trick, but this solution led me to another problem. After using System.exit (0), the onPause (), onStop (), and onDestroy () method of my foreground activity was not called.

Well, that was reasonable behavior for the application, because if you use System.exit (0), the application will be removed from the System process queue, and there will be no way for the android to execute onPause (), onStop () and onDestroy () for mine foreground work.

The workaround I used for this problem was to end my activity when the back button was pressed and after a while kill my application process, as shown below:

 public void killApp(){ final Thread threadKillApp = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } Log.i(TAG, "Going to kill app"); android.os.Process.killProcess(android.os.Process.myPid()); } }); threadKillApp.start(); } 

The call to the killApp () method right after the finish () function was called in my work completed the task.

0
source

try using the onStart () or onResume () method .

Your onCreate method should look like this:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(someView); } 

your onResume method should look like this:

 @Override public void onResume() { super.onResume(); variable = someVariable; } 
-one
source

Check the life cycle of an activity . Do what you want in onResume() instead.

-one
source

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


All Articles