2

たとえば、ユーザーがアプリを開き、ホームボタンを押してから、アプリに戻ったとします。

ユーザーがアプリに戻ったときに特定の機能をトリガーする方法はありますか?ユーザーがアプリに戻ったときにビューオブジェクトを自動ロードするなど。

この質問はAndroidとiOSの両方を対象としています。

4

4 に答える 4

1

プロジェクトのファイルで好みに応じて以下を使用してくださいAppDelegate.m(iOSのみ)

- (void)applicationWillResignActive:(UIApplication *)application
{
    // 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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // 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.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // 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.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // 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.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
于 2012-11-22T11:53:37.700 に答える
1

Androidの場合、backgroudアプリが前面に出たときに呼び出されるonResume関数でコードを修正できます。しかし、Androidのライフサイクルを覚えておいてください

onCreate->onResume。

そのため、onResumeは、アプリが最初に実行された場合やバックグラウンドから実行された場合でも常に呼び出されます。ただし、onCreateは、アクティビティが最初に作成されたときにのみ呼び出されます。

アプリがバックグラウンドになるときに呼び出されるonPauseメソッドに変数を設定できます

そして、その変数「true」を取得してonResumeが呼び出されると、タスクを実行できます。

楽しい。

于 2012-11-22T11:58:02.523 に答える
0

onPause()メソッドでこのコードを記述して、アプリケーションがバックグラウンドに移行したことを確認します。

public void onPause()
{
 ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
Boolean flag=false;     

List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
          ComponentName topActivity = tasks.get(0).topActivity;
          if (!topActivity.getPackageName().equals(context.getPackageName())) {
            flag=true;
          }
        }
if(flag)
{
//App went to background...
}

}

onResume()で上記のフラグを使用して、アプリケーションが再開されたことを確認します。

于 2012-11-22T11:56:47.807 に答える
0

onKeyDownとonPauseの2つのアクティビティメソッドでブール変数を使用してみてください。

boolean backFromBackground = false;

onCreate(){
 //whatever you want
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {     

    if(keyCode == KeyEvent.KEYCODE_HOME)
    {

       backFromBackground = true;
    }
}
onPause(){
 if(backFromBackground){
  //do what ever you want
 }
}
于 2012-11-22T11:59:14.100 に答える