6

I got an app with tabs and a notification bar entry, when I send it to background (click on home button) and try to re-open the application via click on the notification bar, the app restarts (last selected tab is lost).

When I hold the home button if the application is in the background and select it from there or click the app's icon on the homescreen, the previous state is restored per default (the correct tab is selected)

IMO the intent of the notification is wrong, but I'm not sure how to fix it.

In short: How to get a background application back to foreground when I click the notification entry?

thx!

user356764
  • 61
  • 1
  • 1
  • 2
  • Note that firing an `Intent` from a notification always creates a new instance of the `Activity`; you can't return directly to an existing instance. Off the top of my head, I believe you can use one of the flags like CLEAR_TOP (and `onNewIntent` in your `Activity`) to ensure that the existing instance is kept. – Christopher Orr Jun 04 '10 at 14:41

6 Answers6

9

Put these two lines. This will resume currently paused activity:

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
The Hungry Androider
  • 2,274
  • 5
  • 27
  • 52
santhosh
  • 329
  • 3
  • 4
  • 2
    It's works great. But better use constants notifyIntent.setAction(Intent.ACTION_MAIN); notifyIntent.addCategory(Intent.CATEGORY_LAUNCHER); – garmax1 Nov 03 '13 at 19:11
4
Intent intent = new Intent(Application.getContext(), ActivityHome.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Application.getContext().startActivity(intent);
valerybodak
  • 4,195
  • 2
  • 42
  • 53
Kevin Parker
  • 16,975
  • 20
  • 76
  • 105
3

I have faced the same issue and searched vehemently for an answer but here s the trick: Instead of trying to restart the app with the saved state using your notification intent, open a blank activity using the notification intent and in the onCreate() method of the activity, simply finish() it. This will take you back to the last viewed activity on the app.

Abu
  • 553
  • 3
  • 8
  • Omg I am searching for a solution for months, but this is incredibly awesome. This may looks like cheating but awesome. – ertuzun Oct 06 '20 at 20:18
1
public static boolean isApplicationRunningBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(am.getRunningAppProcesses().size());
    for (RunningTaskInfo runningTaskInfo : tasks) {
        if (runningTaskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
            MyLog.i("UTIL", "packageName:" + runningTaskInfo.topActivity.getPackageName());
            MyLog.i("UTIL", "className" + runningTaskInfo.topActivity.getClassName());
            return true;
        }
    }
    return false;
}

Intent notificationIntent;
        if (Util.isApplicationRunningBackground(context)) {
            notificationIntent = new Intent(context, MainView.class);
        } else {
            notificationIntent = new Intent(context, Splash.class);
        }
yasinkafadar
  • 229
  • 1
  • 12
0

use two flags in intent

intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
shantanu
  • 2,408
  • 2
  • 26
  • 56
0

Are you implementing the onSaveInstanceState methods as recommended in the lifecycle documentation?

Its possible that when you pause an application and go back to it immediately, that the application is still hanging out in memory in the background. However, you can't depend on this, so you should save state like the currently open tab everytime you go into the background, and restore it when you get reactivated.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82