简体   繁体   中英

Android sendBroadcast() followed by finish()

I am stuck on something that seems to be a thread synchronization problem. I have two classes:

AppLayout that extends Activity SettingsPart that extends Fragment and implements some Listeners

When the settings are applied successfully I would like to send a Broadcast and finish the Activity, so the code in SettingsPart.java looks like:

((AppLayout) getActivity()).sendBroadcast(intent); ((AppLayout) getActivity()).finish();

The Activity closes as expected, but no broadcast is sent. If I remove the call to finish() the broadcast is sent successfully.

Is there any smart way to wait for the broadcast to be sent before closing the activity?

The Activity is receiving the broadcast, it just can't do anything with it (because the activity is finishing immediately). Instead of calling finish() SettingsPart.java, I would call finish() from wherever you're processing the broadcast within the Activity. Just be careful that you finish the activity in all cases and don't leave it unfinished forever.

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