Calling the finish() method of an Activity from a BroadcastReceiver

隐身守侯 提交于 2020-01-01 17:06:45

问题


I have 2 Activities (Activity1, Activity2) and a BroadcastReceiver class

Assume that we are now on Activity2, where I set up an AlarmManager to run at a specific time.

Is there a way to call the finish() method of Activity2 within the onReceive() of the BroadcastReceiver?

My goal is to return to Activity1 from Activity2 without starting a new Intent in onRecieve().

Note: The BroadcastReceiver class is not registered within the Activity of Activity2. It is registered in the AndroidManifest.xml.


回答1:


You can call finish() on the onReceive method. Do remember to unregister your listener on the OnDestroy. If Activity1 has already been initialized Android will activate Activity1. I am assuming you are opening the activities in a sequence.




回答2:


You can use Activity2class.finish();



来源:https://stackoverflow.com/questions/5153630/calling-the-finish-method-of-an-activity-from-a-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!