简体   繁体   中英

How to finish two instances of activity by finish method

I started activity from service by getting gcm message from servers. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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