活动生命周期随API 25(7.1.1)而变化

Phi*_*Lab 14 android android-intent android-lifecycle android-alertdialog android-7.1-nougat

在我的MainActivity中,如果设置了intent中的标志,我会打开一个对话框.如果对话框已创建,则会被取消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();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果按下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)));
        }
    });
Run Code Online (Sandbox Code Playgroud)

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>
....
Run Code Online (Sandbox Code Playgroud)

sdk版本设置为

minSdkVersion = 19
targetSdkVersion= 22
compileSdkVersion = 23
buildToolsVersion = 23
Run Code Online (Sandbox Code Playgroud)

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

但是在7.1.1.设备 在MainActivity的onNewIntent叫,然后直接事后onPauseonResume.这意味着活动自行打开/到达前台但对话框立即关闭.

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

Tow*_*ihi 2

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

Android 框架可能会在您的 Activity 处于后台或后台堆栈时随时销毁您的 Activity,您应该编写您的 Activity,以便在发生这种情况时它们能够正确运行。看这个 :

不要将活动保留在“开发人员选项”菜单下。启用此选项后,Android 操作系统将在 Activity 停止后立即销毁该 Activity。它旨在帮助开发人员调试他们的应用程序。例如,它可以模拟Android由于内存压力而杀死后台某个Activity的情况。在正常使用中,不建议打开此选项,因为这可能会导致应用程序出现意外问题,例如冻结、强制关闭和重新启动。

您的对话框本身会导致您的活动暂停并关闭。