當我從新片段返回到舊片段時,EditText setText()不顯示任何內容!android:edittext在從新片段返回時不顯示值
從我的FragmentActivity創建FragmentA。從FragmentA創建FragmentB這樣的:
FragmentB fragmentB = new FragmentB();
fragmentB.setTargetFragment(this, "RESULT");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentB);
transaction.addToBackStack(null);
transaction.commit();
從FragmentB我傳遞一個字符串值回FragmentA並返回到FragmentA。
這是我在FragmentB使用的代碼:將RES值傳遞迴順利通過FragmentA的onActivityResult
String res = "OK";
Intent intent = new Intent();
intent.putExtra("RESULT", res);
getTargetFragment().onActivityResult(getTargetRequestCode(), 0xFF, intent);
getFragmentManager().popBackStack();
:
String result;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if ((resultCode == FragmentActivity.RESULT_OK) && (requestCode == 0xFF)) {
result = data.getStringExtra("RESULT");
}
}
然而,當我回去FragmentA的onCreateView,我嘗試以顯示我通過簡單調用從FragmentB移植的結果:
edt_result.setText(result);
但EditText不顯示任何東西!如果我簡單地寫:
edt_result.setText("blah blah"); // The "blah blah" does not appear in my view!
你有什麼想法我可以克服這個問題嗎?
難道是當我回到FragmentA時EditText失去了它的狀態嗎?我試着重寫FragmentA的onSaveInstanceState方法,但是當我離開FragmentA時,onSaveInstanceState根本不會被調用!
任何幫助,高度讚賞。
答案的setText()中的onResume,而不是在onCreateView來電!閱讀更多:http://stackoverflow.com/questions/13303469/edittext-settext-not-working-with-fragment – goseib