Android application front and back switch monitoring

There is a scenario in application development. Click the HOME button on the main page to return to the desktop, and then return to the application home page. When you return to the homepage again, you need to update some data or do some operations.

There are currently three options that can be implemented:

  • Listen for home key events
  • Get the top Activity judgment
  • Register Activity life cycle listener callback

Although the first two schemes can meet the requirements, they have obvious shortcomings and problems. Monitoring the Home button does not guarantee that each model can monitor normally, and different ROMs will have compatibility issues; to obtain the top level Activity after 6.0, you need to open permissions, and it also requires a short round of training judgment in the background, which consumes performance.
Fortunately, the ActivityLifecycleCallbacks class is provided after Android 4.0. It can monitor the life cycle methods of all activities in the application. By observing the difference between the main interface returning to the background and the main interface jumping to other interfaces of the application, you can analyze the front and back switching of the monitoring application. Plan. In this scenario, the ActivityLifecycleCallbacks method callback log is as follows:

1.主页面 -> 桌面 -> 主页面

生命周期如下:... --(点HOME键)--> 主页面#onPause --(重新打开应用)--> 主页面#onResume -> ...

2.子页面 -> 主页面

生命周期如下:... -> 子页面#onPause -> ... -> 主页面#onResume ->...

Therefore, as long as you know that the main page called onPause last time, and this time the main page called onResume, you can determine whether the application is returning from the desktop or other APP. The main code is as follows:

public class AppStatusCallbacks implements Application.ActivityLifecycleCallbacks {

private boolean mMainOnStoped = false;
private boolean mMainOnResumed = false;

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

}

@Override
public void onActivityStarted(Activity activity) {

}

@Override
public void onActivityResumed(Activity activity) {
mMainOnResumed = (activity instanceof MainActivity);
if (mMainOnPaused && mMainOnResumed) {
   // 应用从桌面或者其他应用回来
  
}
}

@Override
public void onActivityPaused(Activity activity) {
 
}

@Override
public void onActivityStopped(Activity activity) {
     mMainOnStoped= (activity instanceof MainActivity);
}

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

}

@Override
public void onActivityDestroyed(Activity activity) {

}
}

In addition, the application generally stores the task stack of the Activity, and the judgment plan can be further optimized by judging the number of the current task stack. When staying on the main interface, it means that the number of Activity's task stack is 1. Add this logic to the above judgment, so that the code will be more fault-tolerant. The complete code is as follows:

public class AppStatusCallbacks implements Application.ActivityLifecycleCallbacks {

private boolean mMainOnStoped = false;
private boolean mMainOnResumed = false;
private int activityCount = 0;

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

}

@Override
public void onActivityStarted(Activity activity) {
  activityCount++;
}

@Override
public void onActivityResumed(Activity activity) {
mMainOnResumed = (activity instanceof MainActivity);
if (mMainOnPaused && mMainOnResumed && activityCount == 1) {
   // 应用从桌面或者其他应用回来
  
}
}

@Override
public void onActivityPaused(Activity activity) {
}

@Override
public void onActivityStopped(Activity activity) {
     mMainOnStoped= (activity instanceof MainActivity);
     activityCount--;
}

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

}

@Override
public void onActivityDestroyed(Activity activity) {

}
}

Guess you like

Origin blog.csdn.net/zhireshini233/article/details/115256250