Activity does not restart on tab changed in android

I am doing an operation using Tab-Host. I have two tabs. When I start the activity of the Host tab, the Host tab opens the action and the activity lifecycle triggers, but when I change the tab and reopen the previous tab, it does not receive its callback methods, such as resume.

+4
source share
3 answers

I don’t think there is any specific reason why it should restart. There is a specific trigger for changing the configuration (for example, rotating the device or extending the keyboard), because the application must deal with the change. But any other process should go according to the Activitvy lifeCycle

When your application goes to the background (loses focus) for any reason, you call onPause() , and when it returns, onResume() called. This is the same when you return home and then back to your application or when you switch such actions. No new intentions or anything like that, just returning to this activity.

You should put your code that should be run in onResume() .

+4
source

Do what you need to do in action in onResume (). It will be called every time, not just the first time it is created.

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

+1
source

When you switch from one tab to another and vice versa, the first tab only gets its onResume method, since it already called its onCreate for the first time.

You can run the code you like in the onResume method if you want something specific to happen when it focuses again.

+1
source

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


All Articles