The onStop() of activity is not called when activity no longer visible

Viewed 321

In a normal situation, as per https://developer.android.com/guide/components/activities/activity-lifecycle#onstop, it is indicated

When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback. This may occur, for example, when a newly launched activity covers the entire screen.

I have an example design below https://github.com/elye/issue_android_activity_not_stop_when_not_visible

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

    override fun onStart() {
        super.onStart()
        Log.d("Track", "MainActivity: onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("Track", "MainActivity: onStop")
    }

    override fun onResume() {
        super.onResume()
        Log.d("Track", "MainActivity: onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d("Track", "MainActivity: onPause")
    }

    fun onButtonClicked(view: View) {
        startActivity(Intent(this, NextActivity::class.java))
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        startActivity(Intent(this, NextActivity::class.java))
    }
}

In this design, the MainActivity will launch NextActivity. In normal launch using the Button to click, the MainActivity does call onStop as it launches the NextActivity

To replicate the case where onStop() of MainActivity is not called when MainActivity is not visible anymore, below is the step

  1. Open up MainActivity (which is singleTask)
  2. Deeplink through DeeplinkActivity (which is singleInstance) using
adb shell am start -a android.intent.action.VIEW -d "https://www.onstoptrigger.com/test"

The DeeplinkActivity will trigger the onNewIntent() of MainActivity. In the onNewIntent(), it will launch NextActivity.

Here, when NextActivity is launched, the MainActivity only called onPause(). The MainActivity's onStop() is not called. Is this expected?

I feel like it's an Android bug but would love to be corrected.

Note:

  1. If I change DeeplinkActivity to singleTask, the problem doesn't occur
  2. If I deeplink directly into MainActivity (without using DeeplinkActivity, the problem doesn't occur too
0 Answers
Related