1

I have an Activity (MainActivity) which contains a Fragment (PlaceholderFragment) with a TextView (myTextView) on it. I try to change TextView's text from MainActivity via below code but always myTextView is null.

my MainActivity class:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
     int id = item.getItemId();

    if (id == R.id.action_settings) {

          PlaceholderFragment myPlace =   mSectionsPagerAdapter.getPlaceholde(1);
          myPlace.setText("New Text");
          return true;
    }

    return super.onOptionsItemSelected(item);
}

my SectionsPagerAdapter class:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        return 4;
    }

    public PlaceholderFragment getPlaceholde(int position) {

      return PlaceholderFragment.newInstance(position);
    }
}

my PlaceholderFragment class:

 public static class PlaceholderFragment extends Fragment {

        private static final String ARG_SECTION_NUMBER = "section_number";
        TextView myTextView;

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {

        }

        public void setText(String s){

            if(myTextView!=null) {
                myTextView.setText(s);
            }else{
                Log.w("myTextView","NULL");    // problem is here: that this line is always launched
            }

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            myTextView =  (TextView)rootView.findViewById(R.id.section_label);
            myTextView.setText("some text");//work well
            return rootView;
        }
    }

}
Artem Stepanenko
  • 3,423
  • 6
  • 29
  • 51

1 Answers1

0

Your issue seems to be in incorrect understanding of how FragmentPagerAdapter and Fragment lifecycle works.

  • Let's start from more basic thing: fragments lifecycle. When call mSectionsPagerAdapter.getPlaceholde(1) you're creating new fragment instance. At this moment Fragments' view is not created yet, so you observe myTextView as null. Based on Fragments' lifecycle a view get created only after onCreateView() callback after the fragment get attached to the activity. In your case this will never happen, because you're creating new fragment (by mSectionsPagerAdapter.getPlaceholde(1)) and not attaching it anywhere.

  • Regarding FragmentPagerAdapter. It creates and caches fragments for you, so you don't need to create and attach every fragment by yourself - pager will keep it. Based on the code it looks like you want to update 1st fragment in the pager on some option selection. Refer to this question on how to do it: basic idea is that you need to override two methods from ViewPager in order to know fragments name and be able to find it using FragmentManager.findFragmentByTag().

Community
  • 1
  • 1
sandrstar
  • 12,503
  • 8
  • 58
  • 65