onCreateView更改未更新

时间:2014-07-23 15:07:36

标签: android android-fragments android-tabhost android-view

我有FragmentTabHost个多个标签。我想在其中一个选项卡中的某些按钮内更改文本,每次用户选择选项卡时,我都无法使其工作。看起来Android正在缓存fragment甚至正在调用onCreateView

我在onCreateView内(作为测试):

static private int i = 0; // this is declared outside the onCreateView method
......
i++;
RadioButton button = (RadioButton) rootView.findViewById(R.id.button);
button.setText(String.valueOf(i));

我已经使用相同的片段类加载了6个制表符,每次我第一次选择制表符时,按钮文本的值都会增加i ....但如果我选择这些制表符中的任何一个另一次,文本保持不变,就像我第一次选择它一样。我知道i正在改变,因为我可以多次选择标签(假设我多次在标签1和标签2之间切换,每次调用onCreateView都被调用并且'i'递增),只是增加i,然后第一次选择另一个选项卡(假设它是选项卡6),并且i的值显示为它应该在该片段内的按钮内,但在此之后,按钮的文本不再更新。

这对我很重要,因为我有一些字符串被翻译成其他语言而且我无法在按钮的语言文本中触发任何更新。

如何强制Android更新按钮的文字?


编辑:

我可以完成移动onResume内的更新,但是我想知道onCreateView内部发生的事情是否有些事情没有得到更新。


编辑2:

这是我的onCreateView代码:

public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         final Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_test_tab, container, false);

........ // configuring some buttons and grids callbacks and animations

i++;
RadioButton button1 = (RadioButton) rootView.findViewById(R.id.button1);
button1.setText(String.valueOf(i));

RadioButton button2 = (RadioButton) rootView.findViewById(R.id.button2);
button2.setText(String.valueOf(i));

RadioButton button3 = (RadioButton) rootView.findViewById(R.id.button3);
button3.setText(String.valueOf(i));

RadioButton button4 = (RadioButton) rootView.findViewById(R.id.button4);
button4.setText(String.valueOf(i));


return rootView;

}

1 个答案:

答案 0 :(得分:0)

那是因为OncreateView()只执行一次,每次选择一个标签时执行的操作都是onResume();

尝试将代码移动到onResume()方法。

public void onResume(){
      i++;
      RadioButton button = (RadioButton) rootView.findViewById(R.id.button);
      button.setText(String.valueOf(i));
}