Search code examples
androidactivity-lifecycle

Notify when onPause() called in any activity of an application


I think this question may simple but I didn't find any solution for this,

I there any way in Android that if any one of an activity calls onPause() I need to show Toast message or any notification kind of thing need to show. Generally I want to get notified when activity calls onPause() but I need it in one place since I may have some 15 activity I don't want to add it in all the activity.

ex:If I have activity when any one of the activity calls onPause I need to get notified but that notification code should be in one place and we should not add any line of code onPause() Is it possible to do this.

Thanks.


Solution

  • Create a baseActivity, which has for example :

    open class BaseActivity : AppCompatActivity() {
    
        override fun onPause() {
            super.onPause()
            Toast.makeText(this, "notified", Toast.LENGTH_SHORT).show()
        }
    }
    

    Then you can extends this in your activities and handle the on pause call in BaseActivity