2012-11-22 26 views
1

例如,用戶打開一個應用程序,按下主頁按鈕,然後再次返回到應用程序。如何在用戶返回後臺應用程序時觸發某個功能?

當用戶導航迴應用程序時,有什麼方法可以觸發某些功能嗎?比如當用戶回到應用程序時自動加載視圖對象。

這個問題是針對Android的iOSiOS。

+0

在Android中,您可以使用onResume()方法。每當你回到你的活動時,都會被調用。請注意,它也會在應用程序啓動時被調用。對於iOS我不知道... – TofferJ

回答

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:. 
} 
+0

非常感謝! –

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()知道您的應用程序恢復。

+0

以上代碼僅適用於Android。 –

+0

在上面的代碼中,context.getPackageName()表示您的應用程序包名稱。 –

+0

非常感謝!我會嘗試一下 –

0

對於android,您可以在onResume函數中正確使用您的代碼,這是在背景應用程序出現前調用的。但請記住android生命週期是

onCreate - > onResume。

so onResume總是被稱爲即使應用程序第一次運行或來自後臺。 ,但onCreate僅在第一次創建Activity時調用。

您可以設置的onPause方法的一些變量時,應用程序將會背景

被稱爲當你拿到變量「真」和所謂的onResume可以執行你的任務。

享受。

0

嘗試在活動性,onKeyDown和onPause兩種方法中使用布爾變量。

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 
} 
} 
+0

這種方法在Android 4.0+版本中不起作用。 –

+0

有什麼解釋? –

+0

它自己不支持Android.For更多細節在Android 4.0設備和Android 2.3.6設備上運行上面的代碼,然後你就會知道原因。 –

相關問題