简体   繁体   English

在android中更改的选项卡上没有重新启动活动

[英]Activity is not restarting on tab changed in android

I am making a activity using Tab-Host. 我正在使用Tab-Host进行活动。 I have two tabs. 我有两个标签。 When I start the tab-Host activity, the tab-Host opens the activity and the life-cycle of the activity is calling but when I changed the tab and again open that previous tab the activity is not getting its callback methods like resume. 当我启动tab-Host活动时,tab-Host打开活动并且活动的生命周期正在调用,但是当我更改选项卡并再次打开前一个选项卡时,活动没有获得其回调方法,如resume。

I don't think there is any specific reason it should restart. 我认为没有任何具体原因应该重启。 For changing configuration (like rotating the device or sliding out a keyboard) there is a specific trigger because the app needs to deal with the change. 对于更改配置(如旋转设备或滑出键盘),有一个特定的触发器,因为应用程序需要处理更改。 But any other process should go according to the Activitvy lifeCycle 但任何其他过程都应该根据Activitvy lifeCycle进行

When your app goes to the background (looses focus) for any reason you get onPause() called, and when it goes back, your onResume() will be called. 当你的应用程序因任何原因进入后台(失去焦点)时,你会调用onPause() ,当它返回时,你的onResume()将被调用。 This is the same for when you go home and then back to your app, or when you switch activities like that. 当你回家然后回到你的应用程序,或者当你切换这样的活动时,这是相同的。 No new intent or something like that, just going back to the activity. 没有新的意图或类似的东西,只是回到活动。

You should put your code that needs to run in the onResume() . 您应该将需要运行的代码放在onResume()

Do what you need to do in the activity in onResume() instead. 在onResume()中的活动中执行您需要做的事情。 That will get called everytime, not just the first time it is created. 每次都会调用它,而不仅仅是第一次创建它。

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

When you switch from one tab to the other and back, the first tab only gets its onResume method called since it has already had its onCreate called the first time. 当您从一个选项卡切换到另一个选项卡并返回时,第一个选项卡只会调用其onResume方法,因为它已经第一次调用了onCreate。

You can run the code you like in your onResume method if you want anything specific to happen when it gets focus again. 如果您希望在再次获得焦点时发生任何特定的事情,则可以在onResume方法中运行您喜欢的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM