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
- Open up
MainActivity(which issingleTask) - Deeplink through
DeeplinkActivity(which issingleInstance) 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:
- If I change
DeeplinkActivitytosingleTask, the problem doesn't occur - If I deeplink directly into
MainActivity(without usingDeeplinkActivity, the problem doesn't occur too