Resume application and stack from notification

2018-12-31 22:08发布

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 the same state as it was before the user left it.

The problem when setting a pending intent on the notification is that it always targets a specific activity. I don't want this. I need to resume the application just as the launcher does.

So if the user is in activity A, I want to resume activity A. If he has launched activity B from activity A, then I want B to be displayed when the user taps the notification, and the stack to be restored so that A gets resumed when the user taps the back button in B.

There are couple of other questions of questions with similar titles, but none address my problem.

标签: android
7条回答
不流泪的眼
2楼-- · 2018-12-31 22:24
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />

private void bringApplicationToForeground(){
    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

      List<ActivityManager.AppTask> tasksList = am.getAppTasks();
      for (ActivityManager.AppTask task : tasksList){
        task.moveToFront();
      }
    }else{

      List<ActivityManager.RunningTaskInfo> tasksList = am.getRunningTasks(Integer.MAX_VALUE);
      if(!tasksList.isEmpty()){
        int nSize = tasksList.size();
        for(int i = 0; i < nSize;  i++){
          if(tasksList.get(i).topActivity.getPackageName().equals(getPackageName())){
            am.moveTaskToFront(tasksList.get(i).id, 0);
          }
        }
      }
    }
}
查看更多
忆尘夕之涩
3楼-- · 2018-12-31 22:26

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.

查看更多
步步皆殇っ
4楼-- · 2018-12-31 22:29

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.

查看更多
十年一品温如言
5楼-- · 2018-12-31 22:36

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.

查看更多
谁念西风独自凉
6楼-- · 2018-12-31 22:42

This is Very Simple open Your manifest file and set attribute Launch mode singleTop in your activity attribute

查看更多
君临天下
7楼-- · 2018-12-31 22:47

I also had the same problem and try to resolve the problem like @GrAnd's answer:

final Intent notificationIntent = new Intent(context,YourActivity.class);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

This works, no doubts about it but the problem is when you set your intent as ACTION_MAIN. Then you will not be able to set any bundle to the intent. I mean, your primitive data will not be received from the target activity because ACTION_MAIN can not contain any extra data.

Instead of this, you can just set your activities as singleTask and call your intent normally without setting ACTION_MAIN and receive the intent from onNewIntent() method of your target activity.

But beaware if you call, super.onNewIntent(intent); then a second instance of the activity will be created.

查看更多
登录 后发表回答