Can you detect when the Android app goes into the background?

Possible duplicate:
How to determine when an Android app goes into the background and comes to the fore

There can be many actions in an Android application. I do not want to know when any old activity goes into the background, I want to know when any activity in the application goes into the background and when any activity in the application comes to the fore.

Do I need to handle onpause / onresume for each activity? Is there an easier way to do this?

+4
source share
3 answers

onPause() and onResume() are override methods. Check Activity Life Cycle

+6
source

If you don’t experience life cycles on Android, you should go through Notepad tutorials. Exercise 3 , in particular, describes in detail the life cycle of an activity

+1
source

Solve the problem with inheritance. To avoid overriding the lifecycle methods of all your actions, redefine them only in your parent.

 public class ParentActivity extends Activity{ public void onPause(){ super.onPause(); // do something } public void onResume(){ super.onPause(); // do something } } public class ChildActivity extends ParentActivity{ // onResume and onPause will be called automatically } 

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1392664/


All Articles