-2

When I close an activity I want it to pass a value to a fragment that is inside another activity, how can I do it?

enter image description here

Activity B

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            boolean value = true;

            Bundle bundle = new Bundle();
            bundle.putBoolean("info", value);
            FragmentA fragment = new FragmentA();
            fragment.setArguments(bundle);
            fragmentTransaction.replace(R.id.idViewPager, fragment).commit();

Fragment A

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {

        Boolean values = getArguments().getBoolean("info");
    }
}

The code is not working, because it gives me an error

java.lang.IllegalArgumentException: No view found for id 0x7f080163

thanks

Progman
  • 16,827
  • 6
  • 33
  • 48
MitchSaver
  • 17
  • 5

1 Answers1

1

You're trying to access a ViewPager that belongs to another activity. If you want to change the fragment in Activity A's ViewPager then Activity A needs to make that decision. If Activity A is launching Activity B, the easiest way to handle this is to use StartActivityForResult for Activity B. Once you set your result (return your value here), make the fragment transaction in Activity A where the result listener receives it.

Hopefully that helps.

Spectre
  • 56
  • 3