How to open last activity from notification status bar?

前端 未结 4 943
庸人自扰
庸人自扰 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.

    0 讨论(0)
  • 2020-12-16 07:21

    Have you tried this:

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

    The above codes instruct Android to open the last activity, whether it's the main activity or a preference activity [that's currently in the foreground] that you used to create/launch the icon in the notification bar in the first place.

    ACTION_MAIN and CATEGORY_LAUNCHER refer to <action android:name="android.intent.action.MAIN"/> and <category android:name="android.intent.category.LAUNCHER"/> directives respectively in your AndroidManifest.xml file.

    0 讨论(0)
  • 2020-12-16 07:29

    Always worked for me.

    Manifest

    launchMode="singleTop" #For Launcher activity
    never use "singleTask" as a launchMode
    set no launchMode for other activities. (if not needed)
    

    Notification Intent

    Intent notificationIntent = new Intent();
    notificationIntent.setClass(this, LaucherActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    
    0 讨论(0)
  • 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<RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0);
    String myPkgNm = getPackageName();
    
    if (!recentTaskInfos.isEmpty()) {
         RecentTaskInfo recentTaskInfo;
         final int size = recentTaskInfo.size();
         for (int i=0;i<size;i++) {
             recentTaskInfo = recentTaskInfos.get(i);
             if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) {
                 nIntent = recentTaskInfo.baseIntent;
                 nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             }
         }
     }
     PendingIntent pi = PendingIntent.getActivity(this, 0, nIntent, 0);
     notification.setLatestEventInfo(getContext(), "some Title", "some text", pi);
     nm.notify(0,notification); 
    

    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.

    :)

    0 讨论(0)
提交回复
热议问题