How to pass data from current chunk to previous chunk

I have 2 Fragments

A and B. I start Fragment B

from Fragment A

. I need to pass data from Fragment B

to Fragment A

to closure Fragment B

. His idea startActivityForResult()

.

Here in Fragment A, I am calling Fragment B:

 FragmentB calendarDialog = new FragmentB();
                calendarDialog.setTargetFragment(CurrentPanchangDialog.this, 1);
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                transaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
                transaction.add(android.R.id.content, calendarDialog).addToBackStack(null).commit();

      

Now, in Fragment B, I call something like this on close:

getTargetFragment().onActivityResult(
                    getTargetRequestCode(),
                    Activity.RESULT_OK,
                    new Intent().putExtra("data", "datafrom C")
            );
            getActivity().getSupportFragmentManager().popBackStackImmediate();

      

Now in the basic operation, I override onActivityForResult()

:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Fragment pFragment = getSupportFragmentManager().findFragmentById(R.id.content);
        if (pFragment instanceof CurrentPanchangDialog) {
            if (pFragment != null) {
                pFragment.onActivityResult(requestCode, resultCode, data);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

      

I am not getting data back to chunk A from chunk B.

+3


source to share


2 answers


â€ĸ Create an interface with one method like this:

public interface FragmentCallback {
    void onDataSent(String yourData);
}

      

â€ĸ Implement this interface in your FragmentA and implement the onDataSent () method:

public class FragmentA implements FragmentCallback {

    @Override
    void onDataSent(String yourData){
        //Do something with your data
    }
}

      

â€ĸ Declare this interface variable in snippet B and create a public setter method:



public class FragmentB {

private FragmentCallback fragmentCallback;

    public void setFragmentCallback(FragmentCallback callback) {
        this.fragmentCallback = callback;
    }
}

      

â€ĸ Somewhere with FragmentA, set it as a callback for the FragmentB instance:

fragmentB.setFragmentCallback(this);

      

â€ĸ Path data from FragmentB to FragmentA (you can do this from one of the fragment lifecycle methods if data needs to be sent when FragmentB is closed):

public class FragmentB {

    public void onDestroy() {
        if(fragmentCallback != null){
            fragmentCallback.onDataSent("Some data from FragmentB");
        }
        super.onDestroy();
    }
}

      

+4


source


Alternatively, you can just use the variable in the Activity and then access it from the Fragment, perhaps something like this:

Activity:

private String data;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

public String getData() {
    return this.data;
}

      



Fragment:

String dataFromActivity = ((YourActivity)getActivity).getData();

      

0


source







All Articles