SoFunction
Updated on 2025-03-11

Detailed explanation of ActivityLifecycleCallbacks instance in Android

Detailed explanation of ActivityLifecycleCallbacks instance in Android

        The above is an example of using ActivityLifecycleCallbacks. The code comments are written very clearly. You can refer to them.

MyApplication is as follows:

package ; 
 
import ; 
 
import ; 
import ; 
import ; 
/**
  * Demo example:
  * ActivityLifecycleCallbacks usage example
  *
  * ActivityLifecycleCallbacks is used to track and callback the life cycle of an activity in the application
  *
  * Use ActivityLifecycleCallbacks to achieve:
  * 1 Determine whether the app is running in the background
  * 2 Close all activities of the app
  *
  *
  * (1) Determine whether the app is running in the background
  * In this implementation, the activityCounter is used to record the number of activities.
  * Determine whether the current APP is running in the foreground by whether the activityCounter is 0.
  *
  * From the implementation method of ActivityLifecycleCallbacks, our inertial thinking is:
  * ActivityCounter+1 in onActivityResumed() of ActivityLifecycleCallbacks.
  * activityCounter-1 in onActivityPaused(),
  * But is that right? 
  *
  * Let's first look at the life cycles of two activities when the interface jumps
  * When jumping from Activity A to Activity B, the life cycles of the two activities are as follows:
  * ()->()->()-> ()-> ()-> ()
  *
  * If as follows:
  * ActivityCounter=1 after A starts;
  * Execute when calling() when jumping from A to B: activityCounter-1=0;
  * That is, the APP is running in the background.
  * Of course this is wrong. The APP is of course running in the foreground during the switching of the two interfaces. 
  *
  * Modifications as follows:
  * In onActivityStarted() for activityCounter+1
  * For activityCounter-1 in onActivityStopped()
  * Summary: Pay attention to the life cycle when the Activity jumps
  *
  * (2) Close all activities of the application
  * 2.1 Use LinkedList<Activity> to manage the interface in the application
  * 2.2 onActivityCreated() in ActivityLifecycleCallbacks
  * Add Activity to LinkedList
  * onActivityDestroyed() in ActivityLifecycleCallbacks
  * Remove Activity from LinkedList
  * 2.3 Destroy all activities in LinkedList when closing the application
  *
  * Note description:
  * 1 ActivityLifecycleCallbacks is only available in API 14 and above
  * 2 Under Android 4.0, you can implement this idea in BaseActivity
  *
  */ 
public class MyApplication extends Application { 
  private static int activityCounter=0; 
  private static MyApplication mApplicationInstance; 
  private static LinkedList&lt;Activity&gt; mActivityLinkedList; 
  private ActivityLifecycleCallbacksImpl mActivityLifecycleCallbacksImpl; 
  @Override 
  public void onCreate() { 
    (); 
    mApplicationInstance=new MyApplication(); 
    mActivityLinkedList=new LinkedList&lt;Activity&gt;(); 
    mActivityLifecycleCallbacksImpl=new ActivityLifecycleCallbacksImpl(); 
    (mActivityLifecycleCallbacksImpl); 
  } 
   
  public static MyApplication getInstance() { 
    if (null==mApplicationInstance) { 
      mApplicationInstance=new MyApplication(); 
    } 
    return mApplicationInstance; 
  } 
   
  //Judge whether the app is running in the background  public boolean isAppRunningBackground(){ 
    boolean flag=false; 
    if(activityCounter==0){ 
      flag=true; 
    } 
    return flag; 
  } 
   
  //Exit the application  public void finishAllActivity(){ 
    //unregisterActivityLifecycleCallbacks(mActivityLifecycleCallbacksImpl); 
    ("--&gt; ()="+()); 
    if(null!=mActivityLinkedList){ 
      for(Activity activity:mActivityLinkedList){ 
        if(null!=activity){ 
          (); 
        } 
      } 
    } 
  } 
   
   
  private class ActivityLifecycleCallbacksImpl implements ActivityLifecycleCallbacks{ 
    @Override 
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) { 
      ("onActivityCreated --&gt; "+().getName()); 
      if (null!=mActivityLinkedList&amp;&amp;null!=activity) { 
        (activity); 
      } 
    } 
 
    @Override 
    public void onActivityStarted(Activity activity) { 
      activityCounter++; 
      ("onActivityStarted --&gt; "+().getName()+",activityCounter="+activityCounter); 
    } 
 
    @Override 
    public void onActivityResumed(Activity activity) { 
      ("onActivityResumed --&gt; "+().getName()); 
    } 
 
    @Override 
    public void onActivityPaused(Activity activity) { 
      ("onActivityPaused --&gt; "+().getName()); 
    } 
 
    @Override 
    public void onActivityStopped(Activity activity) { 
      activityCounter--; 
      ("onActivityStopped --&gt; "+().getName()+",activityCounter="+activityCounter); 
    } 
 
    @Override 
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) { 
      ("onActivitySaveInstanceState --&gt; "+().getName()); 
    } 
 
    @Override 
    public void onActivityDestroyed(Activity activity) { 
      ("onActivityDestroyed --&gt; "+().getName()); 
      if (null!=mActivityLinkedList&amp;&amp;null!=activity) { 
        if ((activity)) { 
          (activity); 
        } 
      } 
    } 
     
  } 
 
} 

FirstActivity is as follows:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
public class FirstActivity extends Activity { 
   private Context mContext; 
   private Button mOpenNewActivityButton; 
   private Button mFinishThisActivityButton; 
   private ClickListenerImpl mClickListenerImpl; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.first_activity); 
    init(); 
  } 
   
  private void init(){ 
    mContext=this; 
    mClickListenerImpl=new ClickListenerImpl(); 
    mOpenNewActivityButton=(Button) findViewById(); 
    (mClickListenerImpl); 
    mFinishThisActivityButton=(Button) findViewById(); 
    (mClickListenerImpl); 
  } 
   
   
  private class ClickListenerImpl implements OnClickListener { 
    @Override 
    public void onClick(View v) { 
      switch (()) { 
      case : 
        Intent intent=new Intent(mContext, ); 
        startActivity(intent); 
        break; 
      case : 
        finish(); 
        break; 
 
      default: 
        break; 
      } 
 
    } 
 
  } 
   
  @Override 
  protected void onResume() { 
    (); 
    //(" first Activity onResume --> "+().isAppRunningBackground()); 
  } 
   
  @Override 
  protected void onStop() { 
    (); 
    //(" first Activity onStop --> "+().isAppRunningBackground()); 
  } 
   
   
} 

SecondActivity is as follows:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class SecondActivity extends Activity { 
   private Context mContext; 
   private Button mOpenNewActivityButton; 
   private Button mFinishThisActivityButton; 
   private ClickListenerImpl mClickListenerImpl; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.second_activity); 
    init(); 
  } 
   
  private void init(){ 
    mContext=this; 
    mClickListenerImpl=new ClickListenerImpl(); 
    mOpenNewActivityButton=(Button) findViewById(); 
    (mClickListenerImpl); 
    mFinishThisActivityButton=(Button) findViewById(); 
    (mClickListenerImpl); 
  } 
   
   
  private class ClickListenerImpl implements OnClickListener { 
    @Override 
    public void onClick(View v) { 
      switch (()) { 
      case : 
        Intent intent=new Intent(mContext, ); 
        startActivity(intent); 
        break; 
 
      case : 
        finish(); 
        break; 
 
      default: 
        break; 
      } 
 
    } 
 
  } 
 
   
} 

ThirdActivity is as follows:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class ThirdActivity extends Activity { 
   private Context mContext; 
   private Button mFinishAllActivityButton; 
   private Button mFinishThisActivityButton; 
   private ClickListenerImpl mClickListenerImpl; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.third_activity); 
    init(); 
  } 
   
  private void init(){ 
    mContext=this; 
    mClickListenerImpl=new ClickListenerImpl(); 
    mFinishThisActivityButton=(Button) findViewById(); 
    (mClickListenerImpl); 
    mFinishAllActivityButton=(Button) findViewById(); 
    (mClickListenerImpl); 
  } 
   
   
  private class ClickListenerImpl implements OnClickListener { 
    @Override 
    public void onClick(View v) { 
      switch (()) { 
      case : 
        finish(); 
        break; 
      case : 
        ().finishAllActivity(); 
        break; 
      default: 
        break; 
      } 
 
    } 
  } 
   
  @Override 
  protected void onResume() { 
    (); 
    //(" third Activity onResume --> "+().isAppRunningBackground()); 
  } 
   
  @Override 
  protected void onStop() { 
    (); 
    //(" third Activity onStop --> "+().isAppRunningBackground()); 
  } 
   
  @Override 
  protected void onDestroy() { 
    (); 
    //(" third Activity onDestroy --> "+().isAppRunningBackground()); 
  } 
 
   
} 

The first_activity.xml is as follows:

<RelativeLayout xmlns:andro 
  xmlns:tools="/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context="" > 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="open new Activity" /> 
 
  <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/openNewActivityButton" 
    android:layout_centerInParent="true" 
    android:layout_marginBottom="50dip" 
    android:textSize="20sp" 
    android:textColor="@android:color/holo_red_light" 
    android:text="This is the first Activity" /> 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/openNewActivityButton" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="50dip" 
    android:text="finish this activity" /> 
 
</RelativeLayout> 

second_activity.xml is as follows:

<RelativeLayout xmlns:andro 
  xmlns:tools="/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context="" > 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="open new Activity" /> 
 
  <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/openNewActivityButton" 
    android:layout_centerInParent="true" 
    android:layout_marginBottom="50dip" 
    android:textSize="20sp" 
    android:textColor="@android:color/holo_red_light" 
    android:text="This is the second Activity" /> 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/openNewActivityButton" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="50dip" 
    android:text="finish this activity" /> 
 
</RelativeLayout> 

third_activity.xml is as follows:

<RelativeLayout xmlns:andro 
  xmlns:tools="/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context="" > 
 
  <TextView 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/openNewActivityButton" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="100dp" 
    android:text="This is the third Activity" 
    android:textColor="@android:color/holo_red_light" 
    android:textSize="20sp" /> 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textView" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="50dip" 
    android:text="finish this activity" /> 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/finishThisActivityButton" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="50dip" 
    android:text="finish All Activity" /> 
 
</RelativeLayout> 

as follows:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:andro 
  package="" 
  android:versionCode="1" 
  android:versionName="1.0" > 
 
  <uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="14" /> 
 
  <application 
    android:name="" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
      android:name=".FirstActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
        <action android:name="" /> 
 
        <category android:name="" /> 
      </intent-filter> 
    </activity> 
     
    <activity android:name=""/> 
    <activity android:name=""/> 
  </application> 
 
</manifest> 

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!