re-open background application via notification it

2019-01-18 14:35发布

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!

6条回答
劳资没心,怎么记你
2楼-- · 2019-01-18 14:51

use two flags in intent

intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
查看更多
Melony?
3楼-- · 2019-01-18 15:00
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);
查看更多
何必那么认真
4楼-- · 2019-01-18 15:05

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.

查看更多
▲ chillily
5楼-- · 2019-01-18 15:07

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

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
查看更多
Anthone
6楼-- · 2019-01-18 15:10
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);
        }
查看更多
欢心
7楼-- · 2019-01-18 15:14

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.

查看更多
登录 后发表回答