Question

I'm working on my 1st Android app and wondering how to handle activation/deactivation/starting/stopping globally, not on Activity level.

This great article shows states transition for Activities: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Is there something similar for Application states?

For example at iOS and Windows Phone app there is clear app states separated from activities (views, controllers, whatever).

I'm asking because I want to perform certain operations only once per app loading/exiting not with every activity starting/stopping

Was it helpful?

Solution

The answer is There is Simply No Direct method to do this

rather than in Application Class you can catch these events

@Override
public void onLowMemory()
{
    super.onLowMemory();
}

@Override
public void onTerminate()
{
    super.onTerminate();
}

So you will have to handle it in all the Activities you will be having

the following methods

  • onResume()
  • onStart()
  • onRestart()
  • onPause()
  • onDestroy()

You will have to implement in all Activity to handle for all application

A suggesstion

You can have some Variable in Application class to save application state

say create a variable like

public static boolean isPaused;

and set it from all activity on state change

OTHER TIPS

The question you're asking is applicable for iOS and Windows but not really for Android.

Android doesn't really have a concept of an application as an object, although there's an Application class. Instead, an app is a loose collection of Activities. There are many good reasons for this state of affairs; for example, it supports fast app switching and easy interaction between Activities of different apps.

The best way to coordinate your "app" so that one Activity doesn't try to do something that's already been done is to use SharedPreferences to store app state. Nearly every other way of doing it is less preferred. Even if the system kills off your entire app, SharedPreferences will maintain the current state. The Application object won't.

Also, Android is based on pausing and resuming. An Activity or activities are created, pause, and resume. They may be destroyed, but that's an extreme case. A corollary to this is that apps should not have an exit button; there's no need for one. I sometimes see apps that have one, but what they're really trying to do is shut down a background Service or process. The best way to do that is to have an affordance that says "Sleep" or similar.

Have all activities inherit from the same hierarchy and put whatever you want in OnCreate, OnPause, OnResume, OnStop, OnDestroy and call the super where applicable.

Example

Parent

IamTheParentActivity : Activity

protected void onCreate()
{
setApplicationState(ApplicationState.Running);
}

protected void onPause()
{
setApplicationState(ApplicationState.Paused);
}

private void setApplicationState(Enum ApplicationState)
{
//Some Application Level Variable
Application.State = ApplicationState
}

Children

IamTheChild : IamTheParentActivity

protected void override onCreate()
{
base.OnCreate;
do other stuff
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top