2

Summary:

ActivityA start ActivityB (ActivityB is singleInstance) ,then click home,ActivityA lifeCycle goto onDestroy?????????????

Detail: I define two activity:

ActivityA:


class ActivityA : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_a)
        button1.setOnClickListener {
            Intent(this, ActivityB::class.java).apply {
                startActivity(this)
            }
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ActivityA">

    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ActivityA"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ActivityB:

class ActivityB : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_b)
    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ActivityB">
    <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ActivityB"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

the manifest is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.coupang.mobile.activitylaunchmodetest">

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity android:name=".ActivityB"
                android:launchMode="singleInstance" />
        <activity android:name=".ActivityA">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Start the app on Android 11 device and click button1 start ActivityB,now there are two task:

taskA:ActivityA

taskB:ActivityB

then click the home button and go to the launcher, the magic happened ,ActivityA destroyed.

why ActivityA was destroyed?

repeat it on Android 10,ActivityA will not be destroyed.

who can tell me why ?

zhang qinglian
  • 1,633
  • 2
  • 11
  • 8
  • If you do this on older Android, you shouldn't end up with 2 tasks. You should have one task with `ActivityB` on top of `ActivityA`. This is because both activities have the same `taskAffinity` and `taskAffinity` trumps `launchMode` (basically `launchMode` of `ActivityB` is ignored. I'm pretty sure they "fixed" this in Android 11, but the fix may be broken :-( – David Wasser Mar 16 '21 at 07:29
  • Try setting `android:taskAffinity=""` on `ActivityB` and see what happens. – David Wasser Mar 16 '21 at 07:30
  • Adding an android:taskAffinity, it will keep the tasks and the activity will not destroy, but as you know it will show 2 different task in in Recent App. This is what we are more concern about, particularly in our app it will not be a user friendly and we are avoiding to use android:taskAffinity. End user should always feel like its a single app and should not get a chance to play with two different task. Below are few concern with android:taskAffinity 1. User will get a chance to play with two different task(Like, Switch between task, Close the any task from background and etc). – zhang qinglian Mar 18 '21 at 05:47
  • You missed my point. If you want to have a single task (which is the correct user experience), you need to get rid of the special launch mode `singleInstance`. Why are you using that launch mode? The special launch modes `singleTask` and `singleInstance` are generall not required and they have a lot of side-effects (one of which you are now seeing) that most developers do not understand. – David Wasser Mar 18 '21 at 10:30

0 Answers0