Skip to content
Advertisement

android – How to detect application being activated

When the app is launched, Application onCreate is called. How to detect when the app is brought to front from running in background?

Advertisement

Answer

Look for onResume() method. Its is always called when your app comes foreground.

As per google docs:

The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states — for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered — so the code in these methods should be fairly lightweight.

CODE SAMPLE:

@Override
public void onResume()
{
    super.onResume();
    Log.d("tag", "This screen is back");
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement