开发者

re-open background application via notification item

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 ther开发者_JAVA百科e 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!


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

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);


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);


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.


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);
        }


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.


use two flags in intent

intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜