简体   繁体   English

每次访问选项卡时如何重新创建活动

[英]How to re-create activity each time the tab is visited

I have a TabActivity , and I want to re-create the activity with the tab is visited(calling the content of onCreate() each visit) . 我有一个TabActivity ,我想用被访问的选项卡重新创建活动onCreate()每次访问都调用onCreate()的内容)。 how ? 怎么样 ?

public void onResume()

This method is called every time an activity comes to the foreground. 每当活动到达前台时,都会调用此方法。 So all you need to do is to override this method 因此,您需要做的就是重写此方法

public void onResume(){
    super.onResume();
   // do your stuffs
}

TO learn more about activity lifecycle see this document 要了解有关活动生命周期的更多信息,请参阅此文档

Now you want to call onCreate each time, it seems you have some initializing task each time the acitivity comes to the foreground. 现在您想每次调用onCreate,似乎每次主动性到达前台时您都有一些初始化任务。 So My suggestion is to use the initialization things in onResume instead of onCreate. 所以我的建议是在onResume中使用初始化的东西,而不是onCreate。

You could use .... 您可以使用....

this.finish(); // this is instance of TabActivity

.... to close the current one and create a new intent using ....关闭当前的并使用创建新的意图

Intent intent = new Intent(this, TabActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 
startActivity(intent);

Edit: Intent.FLAG_ACTIVITY_CLEAR_TOP seems in fact to work as well 编辑: Intent.FLAG_ACTIVITY_CLEAR_TOP实际上似乎也可以工作 will probably not work, because 可能不会起作用,因为 though it is intended to bring an existing activity 虽然旨在带来现有的活动 is just brought 刚带来 to front (without recreation). 向前(没有娱乐)。

Check out this for more. 看看这个更多。

Cheers! 干杯!

Before starting intent of each Tab, set Intent flag "clear top". 在开始每个选项卡的意图之前,将意图标志设置为“ clear top”。

ie

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

check my answer for reference in this question activity-in-tabactivity-doesnt-run-oncreate-method-when-clicked-second-time 检查我的答案以供参考在这个问题上的活动,在-tabactivity -犯规-运行-在OnCreate法-当-点击秒的时间

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

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