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:
- How is it possible it returns null while the first time is okay?
- What is the best solution to change textview/any other control within fragment? There are lots of fragments and I only need to change attached fragment.
nb: All code above are under main activity