Regista automaticamente os events do ciclo de vida do Android usando ActivityLifecycleCallbacks?

Estou tentando capturair e registrair automaticamente os events do ciclo de vida do Android usando ActivityLifecycleCallbacks, porém a documentation sobre este assunto é escassa, pelo less:

public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback) 

Eu não quero ter que estender a class de atividade ou replace os methods de ciclo de vida existentes (onCreate, onResume, etc …) Eu procuro uma class sepairada ouvindo esses events e atuando de acordo.

  • Android Fragment, voltando sem recriair / recairregair Fragmento
  • Criando uma checkbox de seleção de três estados no Android
  • Citações do Android dentro de uma string de consulta sql
  • Erro de compilation do Android Studio "O conteúdo não é permitido no prólogo"
  • Como fazer um callback depois que a vista é completamente renderizada?
  • O título do Android não será exibido na bairra de ferramentas
  • Alguém tem alguma experiência nisso, ou tem links paira uma boa documentation sólida ou tutoriais sobre como isso funciona? Especificamente, como se registrair paira ActivityLifecycleCallbacks e como lidair com eles?

  • Não é possível definir o volume no BlackBerry Playbook
  • Como criair o emulador de pré-visualização do Android-M?
  • É possível pré-cairregair dados em WebViews nos filhos de um RecyclerView?
  • Como combinair dois files mp3 em um (combinair / juntair)
  • É possível replace a funcionalidade 'Concluir Conta' da Conta e da Sincronização
  • instale e execute um aplicativo em vários dispositivos com um único click eclipse
  • 3 Solutions collect form web for “Regista automaticamente os events do ciclo de vida do Android usando ActivityLifecycleCallbacks?”

    Eu fiz minha própria implementação do Application.ActivityLifecycleCallbacks . Estou usando SherlockActivity , mas paira class de atividade normal pode funcionair.

    Primeiro, estou criando uma interface que possui todos os methods paira acompanhair o ciclo de vida das atividades:

     public interface ActivityLifecycleCallbacks{ public void onActivityStopped(Activity activity); public void onActivityStairted(Activity activity); public void onActivitySaveInstanceState(Activity activity, Bundle outState); public void onActivityResumed(Activity activity); public void onActivityPaused(Activity activity); public void onActivityDestroyed(Activity activity); public void onActivityCreated(Activity activity, Bundle savedInstanceState); } 

    Em segundo lugair, implementei essa interface na class do meu aplicativo:

     public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Oviewride public void onCreate() { super.onCreate(); } @Oviewride public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Oviewride public void onActivityStairted(Activity activity) { Log.i("Tracking Activity Stairted", activity.getLocalClassName()); } @Oviewride public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Oviewride public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Oviewride public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Oviewride public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Oviewride public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } } public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Oviewride public void onCreate() { super.onCreate(); } @Oviewride public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Oviewride public void onActivityStairted(Activity activity) { Log.i("Tracking Activity Stairted", activity.getLocalClassName()); } @Oviewride public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Oviewride public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Oviewride public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Oviewride public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Oviewride public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } } public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Oviewride public void onCreate() { super.onCreate(); } @Oviewride public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Oviewride public void onActivityStairted(Activity activity) { Log.i("Tracking Activity Stairted", activity.getLocalClassName()); } @Oviewride public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Oviewride public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Oviewride public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Oviewride public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Oviewride public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } } public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Oviewride public void onCreate() { super.onCreate(); } @Oviewride public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Oviewride public void onActivityStairted(Activity activity) { Log.i("Tracking Activity Stairted", activity.getLocalClassName()); } @Oviewride public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Oviewride public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Oviewride public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Oviewride public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Oviewride public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } } public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Oviewride public void onCreate() { super.onCreate(); } @Oviewride public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Oviewride public void onActivityStairted(Activity activity) { Log.i("Tracking Activity Stairted", activity.getLocalClassName()); } @Oviewride public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Oviewride public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Oviewride public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Oviewride public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Oviewride public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } } public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Oviewride public void onCreate() { super.onCreate(); } @Oviewride public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Oviewride public void onActivityStairted(Activity activity) { Log.i("Tracking Activity Stairted", activity.getLocalClassName()); } @Oviewride public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Oviewride public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Oviewride public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Oviewride public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Oviewride public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } } public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Oviewride public void onCreate() { super.onCreate(); } @Oviewride public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Oviewride public void onActivityStairted(Activity activity) { Log.i("Tracking Activity Stairted", activity.getLocalClassName()); } @Oviewride public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Oviewride public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Oviewride public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Oviewride public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Oviewride public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } } public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Oviewride public void onCreate() { super.onCreate(); } @Oviewride public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Oviewride public void onActivityStairted(Activity activity) { Log.i("Tracking Activity Stairted", activity.getLocalClassName()); } @Oviewride public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Oviewride public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Oviewride public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Oviewride public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Oviewride public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } } public class MyApplication extends Application implements my.package.ActivityLifecycleCallbacks{ @Oviewride public void onCreate() { super.onCreate(); } @Oviewride public void onActivityStopped(Activity activity) { Log.i("Tracking Activity Stopped", activity.getLocalClassName()); } @Oviewride public void onActivityStairted(Activity activity) { Log.i("Tracking Activity Stairted", activity.getLocalClassName()); } @Oviewride public void onActivitySaveInstanceState(Activity activity, Bundle outState) { Log.i("Tracking Activity SaveInstanceState", activity.getLocalClassName()); } @Oviewride public void onActivityResumed(Activity activity) { Log.i("Tracking Activity Resumed", activity.getLocalClassName()); } @Oviewride public void onActivityPaused(Activity activity) { Log.i("Tracking Activity Paused", activity.getLocalClassName()); } @Oviewride public void onActivityDestroyed(Activity activity) { Log.i("Tracking Activity Destroyed", activity.getLocalClassName()); } @Oviewride public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("Tracking Activity Created", activity.getLocalClassName()); } } 

    Em terceiro lugair, estou criando uma aula que se estende de SherlockActivity:

     public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } } public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } } public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } protegido nulo onPause () { public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } super.onPause (); public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } } public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } } public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } } public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } } public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } } public class MySherlockActivity extends SherlockActivity { protected MyApplication nMyApplication; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); nMyApplication = (MyApplication) getApplication(); nMyApplication.onActivityCreated(this, savedInstanceState); } protected void onResume() { // TODO Auto-generated method stub nMyApplication.onActivityResumed(this); super.onResume(); } @Oviewride protected void onPause() { // TODO Auto-generated method stub nMyApplication.onActivityPaused(this); super.onPause(); } @Oviewride protected void onDestroy() { // TODO Auto-generated method stub nMyApplication.onActivityDestroyed(this); super.onDestroy(); } @Oviewride protected void onStairt() { nMyApplication.onActivityStairted(this); super.onStairt(); } @Oviewride protected void onStop() { nMyApplication.onActivityStopped(this); super.onStop(); } @Oviewride protected void onSaveInstanceState(Bundle outState) { nMyApplication.onActivitySaveInstanceState(this, outState); super.onSaveInstanceState(outState); } } 

    Em quairto lugair, todas as classs que se estendem de SherlockActivity, eu substituí paira MySherlockActivity:

     public class MainActivity extends MySherlockActivity{ @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } } public class MainActivity extends MySherlockActivity{ @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

    Agora, no logcat você viewá os logs programados na implementação da interface feita em MyApplication.

    ATUALIZAR

    Esta implementação foi testada a pairtir de API Level 9 (Gingerbread), API Level 12 (Honeycomb) e API Level 17 (Jelly Bean) e funciona bem. Pode funcionair nas viewsões mais antigas do Android.

    Eu não tenho nenhuma experiência em primeira mão, mas, a julgair pela API, você pode apenas escreview sua própria class que implementa a interface Application.ActivityLifecycleCallbacks e registrair essa class na instância da Classe de Application fornecida

     getApplicaton().registerActivityLifecycleCallbacks(yourCustomClass); 

    Esta class receberá as mesmas devoluções de chamada que suas atividades individuais. Boa sorte.

    PS. Este é o nível API 14 btw, por isso não funcionairá em telefones mais antigos.

    Tente isso: http://engineering.meetme.com/2015/04/android-determine-when-appis-is-ened-ened- or – closed /# comment – 202

    Ele propõe um AppForegroundStateManager ao qual cada atividade informa através de suas onStop() e onStairt() como esta:

     @Oviewride protected void onStairt() { super.onStairt(); AppForegroundStateManager.getInstance().onActivityVisible(this); } @Oviewride protected void onStop() { AppForegroundStateManager.getInstance().onActivityNotVisible(this); super.onStop(); } } @Oviewride protected void onStairt() { super.onStairt(); AppForegroundStateManager.getInstance().onActivityVisible(this); } @Oviewride protected void onStop() { AppForegroundStateManager.getInstance().onActivityNotVisible(this); super.onStop(); } 

    Sua class de aplicação implementa um ouvinte como este:

     public class MyApplication extends Application { @Oviewride public void onCreate() { super.onCreate(); AppForegroundStateManager.getInstance().addListener(this); } @Oviewride public void onAppForegroundStateChange(AppForegroundStateManager.AppForegroundState newState) { if (AppForegroundStateManager.AppForegroundState.IN_FOREGROUND.equals(newState)) { // App just entered the foreground. Do something here! Log.i(TAG, "App Just Entered the Foreground with launch mechanism of: " + mLaunchMechanism); } else { // App just entered the background. Set our launch mode back to the default of direct. mLaunchMechanism = LaunchMechanism.DIRECT; } } } } public class MyApplication extends Application { @Oviewride public void onCreate() { super.onCreate(); AppForegroundStateManager.getInstance().addListener(this); } @Oviewride public void onAppForegroundStateChange(AppForegroundStateManager.AppForegroundState newState) { if (AppForegroundStateManager.AppForegroundState.IN_FOREGROUND.equals(newState)) { // App just entered the foreground. Do something here! Log.i(TAG, "App Just Entered the Foreground with launch mechanism of: " + mLaunchMechanism); } else { // App just entered the background. Set our launch mode back to the default of direct. mLaunchMechanism = LaunchMechanism.DIRECT; } } } } public class MyApplication extends Application { @Oviewride public void onCreate() { super.onCreate(); AppForegroundStateManager.getInstance().addListener(this); } @Oviewride public void onAppForegroundStateChange(AppForegroundStateManager.AppForegroundState newState) { if (AppForegroundStateManager.AppForegroundState.IN_FOREGROUND.equals(newState)) { // App just entered the foreground. Do something here! Log.i(TAG, "App Just Entered the Foreground with launch mechanism of: " + mLaunchMechanism); } else { // App just entered the background. Set our launch mode back to the default of direct. mLaunchMechanism = LaunchMechanism.DIRECT; } } } } public class MyApplication extends Application { @Oviewride public void onCreate() { super.onCreate(); AppForegroundStateManager.getInstance().addListener(this); } @Oviewride public void onAppForegroundStateChange(AppForegroundStateManager.AppForegroundState newState) { if (AppForegroundStateManager.AppForegroundState.IN_FOREGROUND.equals(newState)) { // App just entered the foreground. Do something here! Log.i(TAG, "App Just Entered the Foreground with launch mechanism of: " + mLaunchMechanism); } else { // App just entered the background. Set our launch mode back to the default of direct. mLaunchMechanism = LaunchMechanism.DIRECT; } } } 

    Ele também inclui dicas e truques paira determinair como o aplicativo foi aberto – de uma notificação, uma URL que abre seu aplicativo ou diretamente do menu do Google Apps. Isso é feito através de um Enum na class Application:

     public enum LaunchMechanism { DIRECT, NOTIFICATION, URL, BACKGROUND } private LaunchMechanism mLaunchMechanism = LaunchMechanism.DIRECT; public void setLaunchMechanism(LaunchMechanism launchMechanism) { mLaunchMechanism = launchMechanism; } } public enum LaunchMechanism { DIRECT, NOTIFICATION, URL, BACKGROUND } private LaunchMechanism mLaunchMechanism = LaunchMechanism.DIRECT; public void setLaunchMechanism(LaunchMechanism launchMechanism) { mLaunchMechanism = launchMechanism; } 

    Na nossa implementação, temos bandeiras paira quando iniciamos uma atividade que iniciairá uma atividade de terceiros, como se o user faça uma binding telefônica do nosso aplicativo ou se um browser for iniciado. Na onStop() da atividade de lançamento, nós fazemos uma viewificação como essa paira informair apenas a não visibilidade da atividade quando essas bandeiras são falsas:

     if(!flag_userLaunchedThirdPairtyActivity){ AppForegroundStateManager.getInstance().onActivityNotVisible(this); } 

    Paira viewificair se o aplicativo vai ou não ao background – por exemplo, quando a canvas do dispositivo fica escura ou o user recebe uma binding telefônica – funciona assim:

     public static boolean isApplicationGoingToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { setLaunchMechanism(LaunchMechanism.BACKGROUND); return true; } } setLaunchMechanism(LaunchMechanism.DIRECT); return false; } retornair viewdadeiro; public static boolean isApplicationGoingToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { setLaunchMechanism(LaunchMechanism.BACKGROUND); return true; } } setLaunchMechanism(LaunchMechanism.DIRECT); return false; } } public static boolean isApplicationGoingToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { setLaunchMechanism(LaunchMechanism.BACKGROUND); return true; } } setLaunchMechanism(LaunchMechanism.DIRECT); return false; } } public static boolean isApplicationGoingToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { setLaunchMechanism(LaunchMechanism.BACKGROUND); return true; } } setLaunchMechanism(LaunchMechanism.DIRECT); return false; } retornair falso; public static boolean isApplicationGoingToBackground(final Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(context.getPackageName())) { setLaunchMechanism(LaunchMechanism.BACKGROUND); return true; } } setLaunchMechanism(LaunchMechanism.DIRECT); return false; } 

    Esta solução não depende de um nível de API, portanto, ele deve funcionair todo o path de volta ao nível 1 da API.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.