Android application loses state after launching another intention

So, I have the following:

The usual class, in which many of my actions are available in my Android application, by setting the class in my manifest:

<application android:name="com.dev.games.phraseparty.Common"... /> 

Now in this class I have several objects that are created to preserve the state of the application and common services for running applications created in the Common constructor

t

 GameStateVO gameState; public Common() { gameState = new GameStateVO(); } 

My problem is that my activity has an admob ad. When a user clicks on an ad, it causes the web browser to open the URL of the ad in the web browser.

Now, when I click back from a web browser launched by admob, it brings me back to calendar activity and the OnCreate method is called.

Then this operation gets a null pointer exception, because it will do something like:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Common common = this.getApplication(); //null pointer here since the game state VO is null since the Common has lost its state. int score = common.getGameState().getScore(); } 
+1
source share
2 answers

If you do not have active foreground activity, then your process is suitable for shutting down the OS to get more resources. The browser application, in particular, I noticed, uses a lot of resources and can quickly lead to background activity and subsequent processes that will be destroyed. Having a service can help keep your process running, but even then it can be destroyed if necessary. I think you need to use the activity lifecycle cycle method to save and restore state. Learn more about the lifecycle section in the documentation for more information.

+1
source

You might want to learn about the onSaveInstanceState method: this allows you to store any relevant information before it is destroyed. See http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29 for the actual call you need to implement, and Saving Android activity status using Save Instance Status for a great example.

+1
source

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


All Articles