How can I access an Activity from another class globally in an Android app?

Neither Using AlarmManager means that you want the periodic work to continue even after the user exits the activity. Hence, it is very likely that there is no "Playback object", since the user probably is not in your activity. Your service can send its own broadcast Intent to be picked up if the Playback activity is still around This sample project demonstrates using an ordered broadcast for this, so that if the activity is not around, a Notification is raised instead If, on the other hand, you do not want the periodic work to continue if the user gets out of the activity, then do not use AlarmManager Use postDelayed() within the activity, using a Runnable that triggers your service via startService() then reschedules itself via postDelayed() In this case, you can consider using something like a Messenger as a way to have the service let the activity know what is going on, if the activity is still around This sample project demonstrates the use of a Messenger in this fashion.

I have an Android app with 2 activities defined below. You only have one activity. In the MainMenu.oncreate(), I have an AlarmManager kicked off to periodically query a server for data and update the text of a button in the PlayBack UI.

Why? Do you intend for these alarms to go on even after the user exits out of the activity? Can I access the Playback object via a global reference or do I need to kick off the AlarmManager in the Playback.oncreate() instead so I can pass a reference to it?Neither.

Using AlarmManager means that you want the periodic work to continue even after the user exits the activity. Hence, it is very likely that there is no "Playback object", since the user probably is not in your activity. Your service can send its own broadcast Intent to be picked up if the Playback activity is still around.

This sample project demonstrates using an ordered broadcast for this, so that if the activity is not around, a Notification is raised instead. If, on the other hand, you do not want the periodic work to continue if the user gets out of the activity, then do not use AlarmManager. Use postDelayed() within the activity, using a Runnable that triggers your service via startService(), then reschedules itself via postDelayed().

In this case, you can consider using something like a Messenger as a way to have the service let the activity know what is going on, if the activity is still around. This sample project demonstrates the use of a Messenger in this fashion.

The more general problem you are encountering is how to save state across several Activities and all parts of your application. A static variable (for instance, a singleton) is a common Java way of achieving this. I have found however, that a more elegant way in Android is to associate your state with the Application context.

As you know, each Activity is also a Context, which is information about its execution environment in the broadest sense. Your application also has a context, and Android guarantees that it will exist as a single instance across your application. The way to do this is to create your own subclass of android.app.

Application, and then specify that class in the application tag in your manifest. Now Android will automatically create an instance of that class and make it available for your entire application. You can access it from any context using the Context.

This has essentially the same effect as using a static variable or singleton, but integrates quite well into the existing Android framework. Note that this will not work across processes (should your app be one of the rare ones that has multiple processes). EDIT: As Arhimed notes below, this method offers no way easy way of persisting the global state.

If your application finds this necessary you should be using some sort of store; see the Android docs for a variety of methods. Also as anticafe commented, in order to correctly tie your Application override to your application a tag is necessary. Again, see the Android docs for more info.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions