【问题标题】:Is there an method in Android which equivalent to applicationDidBecomeActive?Android中是否有等效于applicationDidBecomeActive的方法?
【发布时间】:2013-04-29 01:57:52
【问题描述】:

我首先尝试使用 onStart() 或 onResume()。但是,使用它们有两个缺点。

1,如果我开始另一个活动并稍后将其关闭,如下所示。 (有点像模态显示一个新的视图控制器,然后将其关闭)

private void dismiss() {
   Intent intent = new Intent();
   intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
   intent.setClass(this, MainActivity.class);
   startActivity(intent);
   finish();
}

onResume() 仍然会被调用

2,我无法在其他活动中重复使用相同的登录信息。

因此,不知Android中是否有一个方法与-(void)applicationWillResignActive:(UIApplication *)application in Android一模一样

【问题讨论】:

  • 你到底想做什么?

标签: android android-activity


【解决方案1】:

你可以使用 onRestart() 方法。仅当您的应用从后台进入前台时才会调用此方法。

这两种方法的相似之处:

类似于“某些案例”中的 appDidBecomeActive。每当您进入后台,即从 Activity 到主屏幕时,都会调用 onPause(),然后调用 onStop() 方法。然后在 android 中恢复到应用程序,然后调用 onRestart() 和 onPause() 方法。

iOs 中的生命周期同时进入主屏幕:(App Delegate 生命周期方法) appWillEnterForeground -> appDidBecomeActive

从主屏幕进入前台时 Android 中的生命周期:(活动生命周期方法) onRestart()->onResume。

从上面可以看出onRestart类似于appWillEnterForeground,onResume类似于appDidBecomeActive;但是我们可以使用 onRestart 而不是 onResume 作为 appDidBecomeActive 因为:

  1. 每次应用程序从一个活动移动到 另一项活动。所以我们最好避免使用Activity(ViewController) 具体方法。此外,onResume 类似于 viewWillAppear iOS 中的方法。
  2. onRestart 方法仅在应用程序来临时调用 从前台到后台,如 appDidBecomeActive 方法,因此更像是应用程序委托方法。

这两种方法的区别:

onRestart() 方法不会在第一次(应用启动时)被调用,因为 appDidBecomeAcitve 被调用。

应用启动期间 iOS 中的生命周期:(应用委托生命周期方法) AppDidFinishLaunchingWithOptions -> appDidBecomeActive.

应用启动期间 Android 中的生命周期:(活动生命周期方法)
onCreate()->onStart()->onResume()

【讨论】:

  • 非常具有误导性的答案。点击后退按钮将触发前一个活动的 onRestart()。根据文档:Called after your activity has been stopped, prior to it being started again。这与 UIApplicationDidBecomeActive 无关
【解决方案2】:

如果您使用dismiss() 代码将MainActivity 放在前面,您可以在MainActivity.onNewIntent() 中检测到这一点,如下所示:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if ((intent & Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
        // I've been brought to the FRONT by another activity
    }
}

这有帮助吗?我不是 100% 确定我明白你想要什么。

【讨论】:

    【解决方案3】:

    由于 Android 中的应用可能由多个活动组成,因此相当于 applicationDidBecomeActive 的情况是没有活动处于运行状态,然后其中一个活动返回。 applicationDidEnterBackground 等价于应用程序的所有活动停止时。

    有一种简单的方法可以使用 ProcessLifecycleOwner 进行跟踪:https://developer.android.com/reference/androidx/lifecycle/ProcessLifecycleOwner

            ProcessLifecycleOwner.get().lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                when (event) {
                    Lifecycle.Event.ON_RESUME -> {
                        Log.i("ProcessLifecycleOwner", "app to foreground")
                    }
                    Lifecycle.Event.ON_PAUSE -> {
                        Log.i("ProcessLifecycleOwner", "app to background")
                    }
                }
            }
        })
    

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 2011-03-20
      • 1970-01-01
      • 2018-07-19
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多