14

In my MainActivity, I have a dialog which is opened if a flag in the intent is set. If the dialog was created, it is dismissed in 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();
    }
}

The dialog is to be opened if a ListView holder's button is pressed and builds an intent 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)));
        }
    });

The AndroidManigfest contains:

<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>
....

The sdk versions are set to

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

On Android < 7.1.1, everything works as expected: onNewIntent() is called and the dialog is visible.

But on 7.1.1. devices the MainActivity's onNewIntent is called, then directly afterwards onPause and onResume. This means that the activity opens itself / comes to the foreground but the dialog was immediately closed.

A possible workaround is to close the dialog in onStop() but I don't get why this happens on Android 7.1.1 - was something changed in the life cycle ?

PhilLab
  • 4,777
  • 1
  • 25
  • 77

2 Answers2

2

It seems that differents are not in the Android version.

If you enable "Don't keep activities" flag in developers settings, then lifecycle will be next:

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

Because onNewIntent always comes in a paused state.

HotIceCream
  • 2,271
  • 2
  • 19
  • 27
2

But on 7.1.1. devices the MainActivity's onNewIntent is called, then directly afterwards onPause and onResume. This means that the activity opens itself / comes to the foreground but the dialog was immediately closed.

The Android framework may destroy your activity any time it's in the background or backstack, and you should write your activities so they behave correctly when this happens. look at this :

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.

Your dialog itself causes your activity to be paused and than closed.

Towfik Alrazihi
  • 536
  • 3
  • 8
  • 25