0

MainActivity.java

FragmentA fragmentA = null;
FragmentB fragmentB = null;

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

}

public void replaceFragment(Fragment newFragment){
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.right_container, newFragment);
    ft.addToBackStack(null);
    ft.commit();
}

public void removeFromActivity(Fragment oldFrag){
    FragmentManager manager = getFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    manager.popBackStack();
    manager.executePendingTransactions();
    ft.remove(oldFrag);
    ft.commit();
}

FragmentA.java

public class FragmentA extends Fragment implements OnClickListener{

public View contentView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if(null == contentView){
        contentView = inflater.inflate(R.layout.fragment_a, null);
        EditText editTxt = (EditText) contentView.findViewById(R.id.right_frag_edit_txt);
        editTxt.setOnClickListener(this);
    }
     //--------------------------------------------------//
     //Here If I am printing
     EditText editTxt = (EditText) contentView.findViewById(R.id.right_frag_edit_txt);
     System.out.println(editTxt.getText()); //Output -- Now Button has Clicked --New value as expected
    return contentView;
}

public void setValues(String newVal){
    EditText editTxt = (EditText) contentView.findViewById(R.id.right_frag_edit_txt);
    editTxt.setText(newVal);
}

@Override
public void onClick(View v) {
    FragmentB newFrag = new FragmentB();
    ((MainActivity)getActivity()).fragmentB = newFrag;
    ((MainActivity)getActivity()).replaceFragment(newFrag);
}
}

FragmentB.java

public class ReplacedFragment extends Fragment implements OnItemClickListener{

public View contentView;

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if(null == contentView){
        contentView = inflater.inflate(R.layout.fragment_b, null);
    }
    return contentView;
}

@Override
public void onClick(View v) {
    FragmentA previousFrag = ((MainActivity)getActivity()).fragmentA;
    previousFrag.setValues("Now Button has Clicked");
    ((MainActivity)getActivity()).removeFromActivity(this);
}
}

From fragmentB when I am clicking a button "Back", I am simply removing this fragment from back stack. As a result FragmentB resumes back. But the edittext value is not updating with new value.

What should I do to update some view of FragmentA when it is resuming back. Please help.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • Refer to my answer on this here: [https://stackoverflow.com/a/53810330/2903196](https://stackoverflow.com/a/53810330/2903196) – albertdadze Dec 17 '18 at 07:06

2 Answers2

1

So if I understand this correctly you would like to update fragment A once you click a button in fragment B. If this is this case I have a solution however it is not very elegant, but it works. I called OnPause of fragment A in fragment B.

Fragment fragmentA = getActivity().getSupportFragmentManager().findFragmentByTag("fragmentA");
fragmentA.onPause();

In OnPause you can now programmatically update your fragment A. You could also in theory call OnResume, but in my case I already used OnResume for its intended purpose.

Max
  • 572
  • 2
  • 6
  • 23
  • Though it is a simple way but there are some good ways of message passing like `broadcast receiver` – Ali Imran Dec 11 '12 at 12:46
  • Well passing variables between fragments is not that simple. One could in theory maybe also use .putArguments(...) .getArguments(...), but that is in this case a bit complicated. – Max Dec 11 '12 at 12:51
  • Could you maybe give an example of how to achieve this? I would also be very grateful to use a different method. – Max Dec 11 '12 at 12:52
  • @Max I don't think it will help, because it will work if you have the new value before leaving the FragmentA. If you want to set the value given by the user in FragmentB, then it will fail. I mean your code is complete static but not dynamic. – Chandra Sekhar Dec 11 '12 at 13:05
  • @Chandra Sekhar Thats true. I use a sqlite database where I can insert the value entered in fragment B and then retrieve it in fragment A. In theory you could save that value in shared preferences. – Max Dec 11 '12 at 13:15
  • @Max Thanks for your suggession, I will try it and let u know. but if u find some better answer, then plz dont forget to share. – Chandra Sekhar Dec 11 '12 at 13:18
  • I will definitely share a different method once I find one. I would love to find a different method, since mine is suboptimal, but currently it is the only one I know :( – Max Dec 11 '12 at 13:20
  • 1
    @Max Thanks for your idea. I will use this for the time being. – Chandra Sekhar Dec 11 '12 at 18:34
1

Finally I found a solution, but I don't know whether its an efficient solution or not. but still I am sharing it.

While starting FragmentB from fragmentA, it will call onPause() of FragmentA where I have set a flag to true. And when FragmentB is removed from FragmentTransaction, it will call to onResume() of FragmentA where I am checking the flag value and updating the FragmentA.

I think this is a better solution, instead of calling lifecycle method onPause() explicitly.

Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • 1
    note that OnResume will not called in fragment A when the back button is pressed. Also OnResume is not called in fragment A if I remove fragment B the following way Fragment fragmentb = getActivity().getSupportFragmentManager().findFragmentByTag("fragmentb"); getActivity().getSupportFragmentManager().beginTransaction().remove(fragmentb).commit(); – Max Dec 14 '12 at 10:10
  • but it still is an interesting approach. – Max Dec 14 '12 at 10:11