Androidでは、フロントバックグラウンドを使ってモニターの切り替えを行います。


前言
最近の仕事では、Androidアプリケーションのバックグラウンド切り替えをどのように実現するために必要とされていますか?詳しく紹介します。
iOS内の端は実現可能であり、App Delegateはフィードバックモニターに与えた:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 func applicationWillResignActive(_ application: UIApplication) {
 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
 }
 func applicationDidEnterBackground(_ application: UIApplication) {
 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
 }
 func applicationWillEnterForeground(_ application: UIApplication) {
 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
 }
 func applicationDidBecomeActive(_ application: UIApplication) {
 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
 }
}
システムのコメントを残しました。iOSの応用周期は、大体の流れはこうです。
アプリケーションはフロントからバックグラウンドに入ります。

applicationWillResignActive() -> applicationDidEnterBackground()
アプリケーションはバックグラウンドからフロントに戻ります。

applicationWillEnterForeground() -> applicationDidBecomeActive()
AndroidにはApplicationも存在しますが、フロントバックグラウンドの切り替えのモニターは提供されていません。
むしろ、Androidでは、バックグラウンドの概念はまったく適用されていません。
Androidの基本ページ単位はActivityです。
Activityは自分のライフサイクルを持っていますが、Applicationにはライフサイクルが一つもありません。
Activityのライフサイクルをモニターすることによって、Applicationのライフサイクルを実現することができます。
Activityのライフサイクルは詳しく述べていません。Androidと書いたのは全部知っているはずです。
私たちは今二つのActivityがAとBであると仮定して、Aは起動ページであると仮定します。ライフサイクルのフィードバックはこのようなものです。
Aが起動されたり、Aがフロントに入ります。

A.onStart()
A.onResume()
AからBへジャンプ:

A.onPause()
B.onStart()
B.onResume()
A.onStop()
BからAに戻る:

B.onPause()
A.onStart()
A.onResume()
B.onStop()
Aがクローズされたか、またはAが楽屋に入る。

A.onPause()
A.onStop()
上の2つのページのコールバックの開始順に注意してください。
onResumeとonPauseはセットで、二つのページの間で順番に呼び出します。
ワンストップとワンストップは一つのグループで、二つのページの間でクロスして呼び出します。
つまり、AはBに起動し、B.onStart()を呼び出してからA.onStop()を呼び出します。BがAに戻ると逆で、A.onStart()を呼び出してからB.onStop()を呼び出します。
この特性を利用して、全Activity.onStart() においてカウンタ+1を記録するグローバルカウンターとしてもいいです。カウンタの数が0より大きいです。フロントに適用されると説明します。カウンタの数は0に等しく、バックグラウンドでのアプリケーションを説明します。カウンタは1から0になり、アプリケーションはフロントからバックグラウンドに入る。カウンタが0から1になり、キャプションアプリケーションがバックグラウンドからフロントに入る。
構想ができたら、私達が実現します。
Applicationは、アプリケーション全体のActivity宣言周期を傍受するためのモニターを提供する。
このモニターはAPI>=14を要求しています。API対応<14の場合は、BaseActivityを作成し、すべてのActivityをこのクラスに統合することにより、アプリケーション全体のActivity宣言周期の傍受を実現することができます。効果は同じです。
API>=14は、以下のように実現される。

public class ApplicationListener implements Application.ActivityLifecycleCallbacks {
 private int foregroundCount = 0; //       Activity    
 @Override
 public void onActivityStarted(final Activity activity) {
  if (foregroundCount <= 0) {
   // TODO                
  }
  foregroundCount++;
 }
 @Override
 public void onActivityStopped(Activity activity) {
  foregroundCount--;
  if (foregroundCount <= 0) {
   // TODO                
  }
 }
 /*
  *     ,      
  */
 @Override
 public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}
 @Override
 public void onActivityResumed(Activity activity) {}
 @Override
 public void onActivityPaused(Activity activity) {}
 @Override
 public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}
 @Override
 public void onActivityDestroyed(Activity activity) {}
}
私たちはアプリでこのモニターを登録して効果を発揮します。

public class MyApplication extends Application {
 @Override
 public void onCreate() {
  super.onCreate();
  registerActivityLifecycleCallbacks(new ApplicationListener());
 }
}
API<14の場合、BaseActivityは以下のように実現される。

public class BaseActivity extends AppCompatActivity {
 private static int foregroundCount = 0; //         
 @Override
 protected void onStart() {
  super.onStart();
  if (foregroundCount <= 0) {
   // TODO                
  }
  foregroundCount++;
 }
 @Override
 protected void onStop() {
  foregroundCount--;
  if (foregroundCount <= 0) {
   // TODO                
  }
  super.onStop();
 }
}
締め括りをつける
以上はこの文章の全部の内容です。本文の内容は皆さんの学習や仕事に一定の助けをもたらすことを望んでいます。もし疑問があれば、メッセージを残して交流してください。ありがとうございます。