How to open last activity from notification status bar?

前端 未结 4 951
庸人自扰
庸人自扰 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 07:31

    Since i didnt get any answer after searching on this, i stopped trying but just some days before i started trying again and i found out the SOLUTION. :) I am putting it here.

    But first of all i just wanted to redefine my problem and it was :: "Getting background task to foreground from status notification bar" i.e., When i put my app to background by pressing home button and then i make it appear to foreground, all activities should come to foreground in the same order as they are in stack of the app.

    For this, i needed a Pending Intent that whenever fired brings my app to foreground.

    Here, as my app starts, a notification is put on the status bar that remains there till my app runs on device.

    So here is the code i tried::

    NotificationManager nm = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);
    Notification notification = new Notification();
    notification.flag = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
    
    Intent nIntent = new Intent();
    
    final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    
    final List recentTaskInfos = am.getRecentTasks(1024,0);
    String myPkgNm = getPackageName();
    
    if (!recentTaskInfos.isEmpty()) {
         RecentTaskInfo recentTaskInfo;
         final int size = recentTaskInfo.size();
         for (int i=0;i

    The main key here is the baseIntent, whose documentation says: The original Intent used to launch the task. You can use this Intent to re-launch the task (if it is no longer running) or bring the current task to the front.

    Documentation of FLAG_ACTIVITY_NEW_TASK says: When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.

    I used it first but it doesnt work as it says and there are many questions on this..

    Anyways.. till now, i found this way good for bringing task to front just as icon on the home screen of android does.

    But please tell me cons of this way so that i can improve.

    :)

提交回复
热议问题