Activity 生命周期更改为 API 25 (7.1.1)

Activity Lifecycle changed with API 25 (7.1.1)

在我的 MainActivity 中,如果设置了意图中的标志,我会打开一个对话框。如果创建了对话框,它将在 onPause()

中被关闭
@Override
public void onPause() {
    super.onPause();
    if (_dialog!= null) {
        _dialog.dismiss();
        _dialog= null;
    }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intentContainsFlag) {
        _dialog = ....;
        _dialog.show();
    }
}

如果按下 ListView 持有者的按钮并构建意图 URI,将打开对话框:

bttn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // The URL scheme is registered in the intent filter
            String intentString = "http://open.example.com/myParameters";
            v.getContext().startActivity(new Intent(Intent.ACTION_VIEW,
                                                    Uri.parse(intentString)));
        }
    });

AndroidManigfest 包含:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:screenOrientation="landscape" >
    <intent-filter>
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.DEFAULT"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <data android:scheme="http" android:host="open.example.com" android:pathPattern=".*"/>
      <data android:scheme="https" android:host="open.example.com" android:pathPattern=".*"/>
    </intent-filter>
....

sdk 版本设置为

minSdkVersion = 19
targetSdkVersion= 22
compileSdkVersion = 23
buildToolsVersion = 23

在 Android < 7.1.1 上,一切都按预期工作:调用 onNewIntent() 并且对话框可见。

但是在 7.1.1 上。设备 调用 MainActivity 的 onNewIntent,然后直接调用 onPauseonResume。这意味着 activity 打开自己/来到前台,但对话框立即关闭。

一种可能的解决方法是关闭 onStop() 中的对话框,但我不明白为什么 Android 7.1.1 会发生这种情况 - 生命周期中发生了一些变化 ?

Android版本似乎没有差异。

如果您在开发人员设置中启用 "Don't keep activities" 标志,那么生命周期将是下一个:

onCreate
onResume
* perform startActivityForResult
onPause
onDestroy
* returning result
onCreate
onResume
onPause
onNewIntent
onResume

因为onNewIntent总是处于暂停状态。

但是在 7.1.1 上。调用 MainActivity 的 onNewIntent 设备,然后直接调用 onPause 和 onResume。这意味着 activity 打开自己/来到前台,但对话框立即关闭。

Android 框架可能会在后台或后台堆栈中随时破坏您的 activity,您应该编写您的活动,以便它们在发生这种情况时能够正确运行。看看这个:

Don't keep activities under the Developer Options menu. When this option is enabled, the Android OS will destroy an activity as soon as it is stopped. It is intended to help developers debug their apps. For example, it can simulate the case that Android will kill an activity in the background due to memory pressure. In normal use, it is not recommended to turn this option on because this may lead to unexpected issues on the apps, such as freezes, force closes and reboots.

您的对话框本身会导致您的 activity 暂停而不是关闭。