I want to resume my app from a status bar notification in the exact same manner as when the user taps its icon in the launcher.
That is: I want the stack to be in th
Just use the same intent filters as Android uses when it launches the app:
final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
As the Intent
you created to open your Activity
from the notification bar is the same as Android used for launching your app, the previously opened Activity
will be shown instead of creating a new one.
This is Very Simple open Your manifest file and set attribute Launch mode singleTop in your activity attribute
For situations where you don't want / can't hard code the launcher activity this solution works
Intent i = getPackageManager()
.getLaunchIntentForPackage(getPackageName())
.setPackage(null)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0);
return new NotificationCompat.Builder(context)
...
.setContentIntent(pendingIntent)
.build();
The setPackage(null) part was key in my case since without it the app did not resume to previous task. I compared my intent with the intent from the Android launcher and noticed that pkg was not set there so that's how I came up with removing package name from the intent.
My particular situation was that the notification was created in a library so there I could not know what the launcher activity would be.
THE ULTIMATE SOLUTION: Notification to restore a task rather than a specific activity?
public class YourRootActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (!isTaskRoot()) // checks if this root activity is at root, if not, we presented it from notification and we are resuming the app from previous open state
{
val extras = intent.extras // do stuffs with extras.
finish();
return;
}
// OtherWise start the app as usual
}
}
Creating an activity and then set the categories and a respective flags... This was the way this worked for me, I had to do it this way cause I did it to support Api lvl 8
intent.addCategory(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(this, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT|
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
and in the AndroidManifest
android:launchMode="singleTask"
So what made the trick was the Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
along with the line setted in the manifest.
Hope it helps to other people.
There could be a more simple way, but you could store the data in the sqlite database and whenever you restart the app whatever states you have saved to the database you can retrieve and set your values to what they need to be at.