ActivityLifecycleCallbacks

Activity lifecycle is one of the most important part of Android development. It managed by activity’s life cycle template methods like onCreate, onStart, onResume and etc. But android provides another useful way : Application.ActivityLifecycleCallbacks

Application.ActivityLifeCycleCallbacks is interface that called for every activity’s lifecycle event. It is almost same as activity lifecycle method but, it can monitor all activity’s lifecycle in a single implemented object. It is something like Spring framework’s AOP.

Normally it’s good choice to implement ActivityLifeCycleCallbacks within Application class. Because it manages all activities in application. 
The callback is registered to Application instance by Application.registerActivityLifecyleCallbaks method. We should implement 8 method of it and each of these are called right after the activity’s lifecycle event.
Below code snippet is basic implementation of ActivityLifeCycleCallbacks within Application class.

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks
{
	@Override
	public void onCreate()
	{
		super.onCreate();
		registerActivityLifecycleCallbacks(this);
	}

	@Override
	public void onActivityCreated(Activity activity, Bundle savedInstanceState)
	{
	}

	@Override
	public void onActivityStarted(Activity activity)
	{
	}

	@Override
	public void onActivityResumed(Activity activity)
	{
	}

	@Override
	public void onActivityPaused(Activity activity)
	{
	}

	@Override
	public void onActivityStopped(Activity activity)
	{
	}

	@Override
	public void onActivitySaveInstanceState(Activity activity, Bundle outState)
	{
	}

	@Override
	public void onActivityDestroyed(Activity activity)
	{
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s