Android – ActivityLifecycleCallbacks

ACTI

Ahora nos encontramos con la necesidad de saber si mi “Activity” se encuentra activa. Bueno para eso sirve “ActivityLifecycleCallbacks”.

Primero debemos crear una clase que herede de “Application” y que implemente la interfaz “ActivityLifecycleCallbacks”.

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {
    private static final String TAG = MyApplication.class.getSimpleName();
 
    @Override
    public void onCreate() {
        registerActivityLifecycleCallbacks(this);
    }
 
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        Log.i(TAG, "onActivityCreated: activity.getLocalClassName = " + activity.getLocalClassName());
    }
 
    @Override
    public void onActivityStarted(Activity activity) {
        Log.i(TAG, "onActivityStarted: activity.getLocalClassName = " + activity.getLocalClassName());
    }
 
    @Override
    public void onActivityResumed(Activity activity) {
        Log.i(TAG, "onActivityResumed: activity.getLocalClassName = " + activity.getLocalClassName());
    }
 
    @Override
    public void onActivityPaused(Activity activity) {
        Log.i(TAG, "onActivityPaused: activity.getLocalClassName = " + activity.getLocalClassName());
    }
 
    @Override
    public void onActivityStopped(Activity activity) {
        Log.i(TAG, "onActivityStopped: activity.getLocalClassName = " + activity.getLocalClassName());
    }
 
    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        Log.i(TAG, "onActivitySaveInstanceState: activity.getLocalClassName = " + activity.getLocalClassName());
    }
 
    @Override
    public void onActivityDestroyed(Activity activity) {
        Log.i(TAG, "onActivityDestroyed: activity.getLocalClassName = " + activity.getLocalClassName());
    }
}

Por último en el archivo manifiesto agregan la clase recién creada

  ...
    <application
        android:name="MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
   ...

Analista/Desarrollador y cofundador de QBit Mexhico. Tecnologías utilizadas Android, Java (EJB, Servlets, Faces, Groovy & Grails, Maven, Swing), .Net (C#, VB, ASP, MVC), Delphi, Spring, Hibernate, DTSx, Oracle, T-SQL, Firebird, MySQL, MongoDB, NodeJs, ReactJs (Flux). Sistemas Operativos Mac, Ubuntu y Windows.

Twitter LinkedIn 

Compartir en...Tweet about this on TwitterPin on Pinterest0Share on LinkedIn0Share on Google+0Share on Facebook1