-1

I am using startActivity(intent) to start an application. I need to know whether the application will be resumed form background or will it be restarted (i.e. it is not in background), before launching the application.

Before Lollipop, I used to get the list of recent tasks and check if it has an application with matching package name.
But in Lollipop I cannot use getRecentTask, so this won't help.

Just for trial, I have also tried:

originalIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);

if(originalIntent != null){
    mainActivityIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    if(originalIntent.equals(mainActivityIntent))
        Log.d("temp", "New Task");
    else
        Log.d("temp", "Background Task");
}

without any success.

What can be done?

Note: The other application I am switching to can be any other application installed on the phone and not necessary my application.

Abhishek
  • 2,959
  • 2
  • 17
  • 24

2 Answers2

1

Use onRestart and onResume method

@Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    }
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
1

When is restarted activity (not in exec) always execute onCreate, when come from background not.

In onCreate method set a variable:

isNewLaunch = true;

And check it in onResume:

if(isNewLaunch) {

} else {

}

Set a false in onPause or onStop

isNewLaunch = false;
Mou
  • 2,027
  • 1
  • 18
  • 29