Reputation: 625
I try to change textview
under attached fragment
on activity so i stored attachedFragment
as variable as code below
@Override
public void onAttachFragment(android.support.v4.app.Fragment fragment) {
super.onAttachFragment(fragment);
attachedFragment = fragment;
}
then when a button is clicked I call following code
if(attachedFragment != null && attachedFragment.getView() != null)
{
TextView tvGender = (TextView) attachedFragment.getView().findViewById(R.id.tv_gender);
if(tvGender!=null)
tvGender.setText(R.string.title_step_one_gender);
}
When I start the activity and it works fine until i changed into the next fragment and pressed back; the attachedFragment.getView()
always returns null
My question:
nb: All code above are under main activity
Upvotes: 0
Views: 462
Reputation: 5093
Please correct me if I misunderstood your question. It sounds like your situation is, you attach fragment A, then you attach fragment B, then you press back, leaving you with fragment A. In this case, attachedFragment is just a variable, so it continues to point to B, but since B is now detached, it is null. Pressing back will not repopulate the variable attachedFragment with fragment A.
Try using findFragmentById or findFragmentByTag. Check out this thread for more info.
Upvotes: 1