6

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 notification to notification status bar. activity A also opens an activity B and B opens another activity C. From C i press home button. Now i want to go again to my app so from notification bar i tap on notification (which was sent by A). Here the notification should start activity C because it was last opened.

I did search on this but didn't find proper answer. Thanks in advance.

android_one
  • 425
  • 5
  • 10

4 Answers4

24

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.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
android_one
  • 425
  • 5
  • 10
  • Works like a charm, i used a login activity that opens another activity based on some parameters. Now when a user opens the app by tapping the notification, it now reopens the app without stacking the activities! – Oritm Oct 17 '13 at 12:35
  • 1
    I want to add, use android:launchMode="singleTop" in manifest and use Intent.FLAG_ACTIVITY_NEW_TASK in intent. – shantanu Aug 05 '15 at 20:58
7

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.

:)

ChuongPham
  • 4,761
  • 8
  • 43
  • 53
android_one
  • 425
  • 5
  • 10
5

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.

ChuongPham
  • 4,761
  • 8
  • 43
  • 53
2

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);
shantanu
  • 2,408
  • 2
  • 26
  • 56