2

例如,用户打开应用程序,按下主页按钮,然后再次返回应用程序。

当用户导航回应用程序时,有什么方法可以触发某些功能?例如当用户返回应用程序时自动加载视图对象。

这个问题适用于 Android 和 iOS。

4

4 回答 4

1

对于 android,您可以在 onResume 函数中纠正您的代码,该函数在后台应用程序出现时调用。但请记住 android 生命周期是

onCreate-->onResume。

所以 onResume 总是被调用,即使应用程序第一次运行或来自后台。但 onCreate 仅在第一次创建 Activity 时调用。

您可以在应用程序进入后台时调用的 onPause 方法上设置一些变量

当您获得该变量“true”并调用 onResume 时,您可以执行您的任务。

请享用。

于 2012-11-22T11:58:02.523 回答
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 回答
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 中使用布尔变量。

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 回答