简体   繁体   中英

Android - Catching onPause without being the active Activity

Is there any way to catch onPause() in a java class that is Not an activity? I have native code with a Java-helper class that is used in other apps, and whenever those apps enters background I want to prepare for it too. How can I solve this? Is it enough to have my Java class extend some class to get the onPause(), even though its not registered as an activity in manifest etc?

Today we can use registerActivityLifecycleCallbacks() of the Application class and provide onActivityPaused().

Example:

AppContext.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
  @Override  
  public void onActivityPaused(Activity activity) {
    //handle here
  }
}

创建一个名为的公共方法onPause()prepare()whateverSuitYou()在助手类,并从调用它onPause()你的Activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM