How to open last activity from notification status bar?

前端 未结 4 945
庸人自扰
庸人自扰 2020-12-16 06:35

I want to open last started activity by tapping on the notification in status bar. Suppose I start an Activity A (main activity of my app), this activity sends a notificatio

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 07:13

    Few days back I got very very simple solution for my problem. Instead of iterating through recentTasks and getting our task and then getting baseIntent through it, we can do simple thing as follows:

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    

    baseIntent contains the same parameters as above Intent has. Hence instead of grabbing baseIntent from recentTasks, it's quite good to use above code.

    This notificationIntent will then be passed to pendingIntent for further use.

    Provided: MainActivity is the very first activity when we launch our app and in AndroidManifest.xml it must contain IntentFilters of CATEGORY_LAUNCHER and ACTION_MAIN.

提交回复
热议问题