Android: update the previous fragment data when you click on the button

I have 2 fragments, fragment A and fragment B. I added fragment B over fragment A using FragmentTransaction().add , which means that fragment A is at the base of fragment B. Is there a way to change the data in fragment A after how did i do something on fragment B and hit the back button from fragment B? I want to have a common way to notify fragment A. Because it may be another fragment superimposed. I tried using FragmentTransaction.replace() - it works fine to refresh the previous page.

+4
source share
1 answer

Just rewrite onBackPressed() in your activity and snippet and complete the calls you need.

More for callbacks / links to other snippets can be found here:

Communication with other fragments

 public class FragmentA extends Fragment { public void updateMyself(String updateValue){ Log.v("update", "weeee Fragment B updated me with" + updateValue); } } public class FragmentB extends Fragment { public Interface FragmentBCallBackInterface { public void update(String updateValue); } private FragmentBCallBackInterface mCallback; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (FragmentBCallBackInterface) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement FragmentBCallBackInterface"); } //As an example we do an update here - normally you wouln't call the method until your user performs an onclick or something letsUpateTheOtherFragment(); } private void letsUpateTheOtherFragment(){ mCallback.update("This is an update!); } } public class MyActivity extends Activity implements FragmentInterfaceB { @Override public void update(String updateValue){ FragmentA fragmentA = (FragmentA) getSupportFragmentManager().findFragmentById(R.id.article_fragment); if (fragmentA != null) { fragmentA.updateMyself(updateValue); } else { //replace the fragment... bla bla check example for this code } } } 
+4
source

All Articles