0

I have an Activity that has a section where a Fragment is shown. A button is clicked on the Fragment and a new Activity launches with some buttons. Depending on which button is clicked, that activity closes and the Fragment should be updated accordingly but I'm having trouble accessing the views on the Fragment to edit them.

It seems that in the onResume() of the Fragment, I'm unable to access the view using getView(). I've also tried passing in the view from the Activity that the Fragment lives in, but that also returned null.

So I'm wondering if Fragments are static after onCreateView() is called?

SecondActivity calling back into MainActivity:

@Override
protected void onResume() {
    super.onResume();
    rvAdapter.setOnItemClickListener(new RVAdapter.MyClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtra("key", value);
            startActivity(intent);
        }
    });
}

MainActivity handling the call back and calling a function in the Fragment:

@Override
protected void onResume() {
    super.onResume();
    Intent intent = getIntent();
    String value = intent.getStringExtra("key");
    if (value != null) {
        ((ExampleFragment) exampleFragment).updateFragment(value);
    }
}

Fragment attempting to update itself:

public void updateFragment(String value) {
    TextView textView = (TextView) getView().findViewById(R.id.profile_text_view);
    if (value != null) {
        textView.setText(value);
    }
}

And the result would be NPE on getView() call in the Fragment. I've tried passing in the view from MainActivity via: exampleFragment.getView() but also no luck.

jerbotron
  • 307
  • 1
  • 4
  • 14

0 Answers0