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 ?

2
  • Can you post a simple project with that behavior at github?
    – azizbekian
    Sep 6, 2017 at 13:24
  • Could it be that your dialog itself causes your activity to be paused?
    – Nonos
    Sep 12, 2017 at 17:46

2 Answers 2

2
+50

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.

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.

1
  • Why onStart() is not called?
    – azizbekian
    Sep 13, 2017 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.