1

I have tried all the methods mentioned in the following thread How to access Fragment's child views inside fragment's parent Activity? but none of them seem to work for me. Only after trying hard for an hour and a half, I am asking this question.

My CategoriesFragment

    public class CategoriesFragment extends Fragment {

    private TextView textView;

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_categories,container,false);
            textView = view.findViewById(R.id.cat_text);
            Log.i("Tag",textView.getText().toString());
            return view;
        }

        public TextView getTextView(){
            return textView;
        }

         .

         .

         .

 }

I am opening the categories fragment when the user clicks on a bottom navigation view consisting of a categories tab.

My HomeScreen Activity

public class HomeScreen extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener, CategoriesFragment.OnFragmentInteractionListener{

 private BottomNavigationView bottomNavigationView;
 private CategoriesFragment categoriesFragment;
 private FrameLayout bottomFrameLayout;
 private TextView textView;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);

        bottomNavigationView = findViewById(R.id.bottom_nav_view);
        bottomFrameLayout = findViewById(R.id.bottom_nav_frame);
        categoriesFragment = new CategoriesFragment();
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
}

@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
             case R.id.bottom_nav_categories :
                getSupportFragmentManager().beginTransaction().replace(R.id.bottom_nav_frame,categoriesFragment).addToBackStack(null).commit();
                textView = categoriesFragment.getTextView();
                textView.setText("DONE"); 
        }
        return true;
}

This throws a null pointer exception, as it is not being able to get the Text view. Any help/guidance would be much appreciated :)

RKRK
  • 1,284
  • 5
  • 14
  • 18
CS Learner
  • 231
  • 2
  • 11
  • 1
    you can pass argument done to fragment and then set the text to textview in fragment itself https://stackoverflow.com/questions/17436298/how-to-pass-a-variable-from-activity-to-fragment-and-pass-it-back – Raghunandan Jun 25 '19 at 05:23
  • I understand what you're saying. I will try this out. But, is there no way to do the same inside my activity class? – CS Learner Jun 25 '19 at 05:28

2 Answers2

0

The commit()function does not work synchronously, so your fragment will not be ready directly after the call to it.

Taken from the documentation.

Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready.


Use commitNow() as this works synchronously and the fragment is fully brought to the lifycycle state of their host.

See https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#commitnow for further information

TreyWurm
  • 495
  • 5
  • 16
-1

Why getting textview and setting text from activity when you can pass the string as parameter to the public method of fragment to change text?

  • @TaQuangTu I dont think it is good procedure to expose view of a fragment to an activity and allowing to make changes. It is only a matter of passing a parameter to the fragment. The text value will be changed when views are created. – Pramod Moolekandathil Jun 25 '19 at 11:23