简体   繁体   English

如何通过finish方法完成两个活动实例

[英]How to finish two instances of activity by finish method

I started activity from service by getting gcm message from servers. 我通过从服务器获取gcm消息来启动服务活动。 If I get the message twice, than activity starts two times and if I click on back button and call finish() method than above activity gets closed. 如果两次收到该消息,则活动开始两次,如果单击后退按钮并调用finish()方法,则以上活动将关闭。 And then again click back button no activity happens. 然后再次单击“后退”按钮,则没有任何活动。

Intent notificationIntent = new Intent(context,
                NotificationAcceptBookingActivity.class);
            notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            notificationIntent.putExtra("bookingId", booking.getBookingId());
            getApplication().startActivity(notificationIntent);ss

Don't try to create new instance of the activity again and again. 不要尝试一次又一次地创建活动的新实例。 Before starting the activity just close it if exists. 在开始活动之前,请将其关闭(如果存在)。

You shouldn't be having more than one instances of an activity alive at a time. 您一次不应存在一个以上活动的多个实例。 However, if you still want it, maybe clearing the activity stack can help you with it. 但是,如果仍然需要,清除活动堆栈可能会帮助您。 When starting the last activity, use 开始上一个活动时,请使用

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

to clear previous activity stack, which will kill 2 (or no matter how many) instances of the activity. 清除先前的活动堆栈,这将杀死2个(或无论多少个)活动实例。

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

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