获取恢复活动的Intent

4

Activity.getIntent返回创建该Activity的Intent。

在Activity从onPause到onResume的情况下,有没有办法获取恢复Activity的Intent?


你是指从你的应用程序中打开的活动中导航回来,并希望知道它是从哪个活动返回的吗?请参阅 OnActivityResultstartActivityForResult - IAmGroot
不,我指的是从与通知相关联的PendingIntent恢复Activity的情况。 在这种情况下,Activity可能不会经历整个生命周期:首先调用onResume,在此时,我没有看到任何方法可以找出它是否被从通知中恢复。 - david
2
关于Activity的onNewIntent()方法怎么样?http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) - Gopal Gopi
谢谢Gopal,onNewIntent完美运行。 - david
2个回答

3
解决方法是覆盖Activity.onNewIntent方法,在onResume之前调用该方法。

0

我找到了如何添加此代码的方法:

notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

现在应该是这样的:

    notification = new Notification(R.drawable.icon,
        "Notify", System.currentTimeMillis());
notification.setLatestEventInfo(this, "App name",
        "App message", PendingIntent.getActivity(this,
                0, new Intent(this, Main.class)
                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags |= Notification.FLAG_ONGOING_EVENT;

谢谢Mahmoud的反馈,我已经按照@Gopal的建议通过重写onNewIntent解决了我的问题。 - david

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接