Android WebView is calling onActivityStopped method on App Class

I need to do an API request to let a service know when the app goes to background.

The problem is when I open an activity that contains a WebView inside.

I don't know why when this activity is opened the onActivityStopped() from the App class is called, so I have this:

App.kt

class App: Application() {
    override fun onCreate(){
        super.onCreate()
        ....
        // some logic here
        ....
        registerActivityLifecycleCallbacks(AppLifecycleTracker())
}

    inner class AppLifecyclerTracker: ActivityLifecycleCallbacks {
        override fun onActivityStopped(Activity: Activity){
            // some logic here
        }
    }

}

so, if in the MainActivity.kt I call the WebViewActivity.kt, that onActivityStopped method will be called and I want to that method be called when the app goes to background only

btw everything works like a charm in the others classes that do not contain WebView

Answers 1

  • If MainActivity starts any activity, MainActivity will be stopped. I would expect onActivityStopped() to be called, with your MainActivity instance as a parameter, to reflect this lifecycle event.

    I want to that method be called when the app goes to background only

    Use ProcessLifecycleOwner to determine when your app's UI moves to the background.


Related Articles