简体   繁体   English

Android如何创建恢复活动的通知,而不是开始新的onw

[英]Android how to create notification that resumes activity instead of starting a new onw

So I've read the varios SO questions about this and I'ved tried to implement what they said and it doesnt work. 因此,我已经阅读了有关此问题的各种问题,并尝试实现他们所说的内容,但它不起作用。 When the notification is pressed i want it to pull back to the activity without clearing any of the EditTexts or turning off the toggle button. 当按下通知时,我希望它返回到活动而不清除任何EditText或关闭切换按钮。 Ie resuming the activity from its last state. 即从最后状态恢复活动。 But so far my code doesnt work. 但是到目前为止,我的代码无法正常工作。

Intent myIntent = new Intent(this,Locator.class);
 PendingIntent pendingIntent
                = PendingIntent.getActivity(getBaseContext(),
                0, myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                Intent.FILL_IN_DATA);
        myNotification.defaults |= Notification.DEFAULT_SOUND;
        myNotification.defaults |= Notification.DEFAULT_VIBRATE;
        myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

The Android OS is solely responsible for managing the lifecycle of an Activity and can kill an Activity at any time, thus there is no way to specify resume activity. Android操作系统仅负责管理活动的生命周期,并且可以随时终止活动,因此无法指定恢复活动。 The OS will bring the requested activity to the top of the stack if it is still in memory; 如果仍在内存中,则操作系统会将请求的活动移到堆栈的顶部; if the activity has been killed, it will be launched again and any saved state passed to it. 如果该活动已被终止,则将再次启动该活动,并将任何保存的状态传递给该活动。

See Recreating an Activity 请参阅重新创建活动

maybe 也许
<activity name="your activity" android:launchMode="singleInstance" />
is what you want. 是你想要的。 If activity not destroyed and in background, then it will appear, instead creating new. 如果活动没有在后台被销毁,那么它将出现,而是创建新的活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM