goseib
goseib

Reputation: 737

android: edittext in fragment not showing value when returning from new fragment

When I return from the new Fragment to my old Fragment, the EditText setText() does not show anything!

From my FragmentActivity I create FragmentA. From FragmentA I create FragmentB like this:

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();

From FragmentB I pass a string value back to FragmentA and return back to FragmentA.

This is the code I use in FragmentB:

String res = "OK";
Intent intent = new Intent();
intent.putExtra("RESULT", res);
getTargetFragment().onActivityResult(getTargetRequestCode(), 0xFF, intent);
getFragmentManager().popBackStack();

The res value is passed back successfully via FragmentA's onActivityResult:

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");
    }
}

However, when I go back to FragmentA's onCreateView, I try to show the result I ported from FragmentB via a simple call:

edt_result.setText(result);

but the EditText does not show anything! The same happens if I simply write:

edt_result.setText("blah blah"); // The "blah blah" does not appear in my view!

Do you have any idea how I can overcome this problem?

Could it be that the EditText loses its state when I go back to FragmentA? I tried to override FragmentA's onSaveInstanceState method but onSaveInstanceState is not called at all when I leave FragmentA for FragmentB!

Any help is highly appreciated.

Upvotes: 2

Views: 2152

Answers (1)

goseib
goseib

Reputation: 737

The answer is to call setText in onResume and not in onCreateView. Read here for more:

EditText Settext not working with Fragment

Upvotes: 3

Related Questions