Android inter-Activity signalling (newbie question)
I'm writing开发者_开发问答 an app that has a main Activity which controls the main display that the user views and interacts with.
It also has an options menu and each menu item has its own Activity and Layout to perform various subordinate tasks. Some of these tasks may result in changes to the contents of the main display. So I want those subordinate Activities to be able to signal the main Activity that it has to redraw or recompute the main display the next time control is transferred to the main Activity, say, it its onResume() handler.
What's a lightweight way for one activity to signal to another that it has to do something. I come from a Windows background so I'm looking for the equivalent of sending a WM_PAINT message, or setting a flag or semaphore for the main activity to read.
I tried just putting a public boolean in the main Activity but it complained that it had to be static. Thanks in advance!
The way two components of an android app talk to each other is with broadcasts and broadcast receivers. http://developer.android.com/reference/android/content/BroadcastReceiver.html
A broadcast receiver can either be registered in the manifest, or in code dynamically (although make sure to unregister it or the log will complain about leaking broadcastreceivers).
When you register the receiver, you also need to provide an intent that matches the intent you used to make the broadcasat.
Another possible way you could do it is if you call the one activity directly from another, you could call the startActivityForResult() method: http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int).
Oh, also just a recommendation, every android app has a global sharedpreferences that you can tap into, so you could just check that whenever you redraw.
精彩评论