Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity Lifecycle changed with API 25 (7.1.1)

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 ?

like image 780
PhilLab Avatar asked Aug 08 '17 12:08

PhilLab


People also ask

What are the different phases of the activity life cycle?

Activity-lifecycle concepts To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .

What are the different phases of activity lifecycle in Android?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .

Which of the following activity life cycle methods is called once the activity is no longer visible?

If the activity stops being visible to the user, the onStop() method gets called. After the onStop() method has run, the activity is no longer visible. If the activity becomes visible to the user again, the onRestart() method gets called, followed by onStart() and onResume().

What is the difference between onPause and onStop Android?

onStop() is called when the activity is has already lost the focus and it is no longer in the screen. But onPause() is called when the activity is still in the screen, once the method execution is completed then the activity loses focus. So, onPause() is logically before onStop().


1 Answers

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.

like image 112
Towfik Alrazihi Avatar answered Sep 25 '22 13:09

Towfik Alrazihi