How to run code every time the application opens Android

I want to execute a piece of code (say, for example, display Toast) every time the application is open. So far, I have done this every time the application starts, putting the code in my MyApp.java file, which extends the application.

However, if I click on the main screen or exit the application and then go to it, the message does not appear again. This only happens when I restart the application. Any idea how to do this?

EDIT: Basically, I ask how to execute the code every time the whole APP is brought to the forefront (this can be opened the first time after another application has been used, after the user has abandoned the application, etc. ) Where would I place the onResume code? This would not be in any specific action, if I wanted it to be applied when an integral application appeared in the foreground, and not just specific activity.

+4
source share
2 answers

You can try writing this code to your @ Override-d onResume () method.

0
source

You should use the onResume callback:

Android API

An example using the previous SO question

In the activity class:

@Override
protected void onResume() {
    super.onResume();
    //your code here
}
0
source

All Articles