0

I have 3 fragments. Fragment A, B and C. A have a "continue" button which will take it to B. B have a proceed button which will take it to C. C have a "add" button which will take it back to B. Now I want to send data from A to B when the continue button is pressed. and also from C to B when the add button is pressed. I tried using bundle. It is giving me null pointer exception as the first time when going from A to B , the bundle from C is null. How to solve this? Any help is highly appreciated. Please go through the code snippet below

Note: ItemDetails is obtained from fragment A and EmployeeDetails is obtained from fragment C. Fragment Flow => 1. fragment A 2. A to B(itemsList passed to B) 3. B to C (No communication) 4. Back to B from C(Employee List passed to B).

 String TEMP_STRING_EMPLOYEES, TEMP_STRING_ITEMS;
EmployeeList employeeList;
ItemsList itemsList;

@Override
public void onStart() {
    super.onStart();
    Bundle args = getArguments();
    if (args != null) {

        TEMP_STRING_ITEMS = args.getString("ItemsDetails");

        try {
            // Set article based on argument passed in
            TEMP_STRING_EMPLOYEES = args.getString("EmployeeDetails");

        } catch (NullPointerException ex) {

        }


    } else {

    }

}
//Next lines of code from MAinActivity.java

@Override public void onFragmentInteractionForEmployeeDetails(ArrayList arrayList) {

    EmployeeList employeeList = new EmployeeList(arrayList);
    String correspondingJson = NavigationUtils.getStringForObject(employeeList);

    B newFragment = new B();
    Bundle args = new Bundle();
    args.putString("EmployeeDetails", correspondingJson);
    newFragment.setArguments(args);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack so the user can navigate back
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}
  • Take a look at [this](https://stackoverflow.com/a/44008851/6950238) – Andrii Omelchenko Jun 20 '17 at 08:22
  • Can you post your bundle code, sending and receiving? – Pratik Popat Jun 20 '17 at 08:35
  • For communication between Fragments, you can either [circulate via Activity](https://developer.android.com/training/basics/fragments/communicating.html) or follow [this answer](https://stackoverflow.com/a/36496481/1276636) – Sufian Jun 20 '17 at 08:44
  • Can you add the part where your set the arguments to the Fragment? – Eselfar Jun 20 '17 at 08:48
  • @Eselfar done.. – Abdul Kader Jun 20 '17 at 08:56
  • that onStart() where it is written? can you paste full code? – Pratik Popat Jun 20 '17 at 09:01
  • Seems correct. Are you sure your `correspondingJson` is not null? Have you checked the return of `NavigationUtils.getStringForObject(employeeList);` – Eselfar Jun 20 '17 at 09:02
  • Have a look at http://square.github.io/otto/ – michalsol Jun 20 '17 at 09:16
  • @michalsol no need for Otto for such a trivial task. – Sufian Jun 20 '17 at 10:32
  • @AbDuLkAdeR with [this solution](https://stackoverflow.com/questions/36495842/is-there-a-method-like-setresult-in-fragement/36496481#36496481) you will be able to achieve what you're trying to do. When A opens B, you will use `onActivityCreated()` or the likes to retrieve the data. When C closes returns data to B, you'll use B's `onActiivtyResult()`. – Sufian Jun 20 '17 at 10:48
  • Possible duplicate of [Is there a method like setResult() in fragement?](https://stackoverflow.com/questions/36495842/is-there-a-method-like-setresult-in-fragement) – Sufian Jun 20 '17 at 10:49
  • @Sufian Yes, I agree with you :) However, I believe it's worth looking into that tool. Now I'm just using it automatically :D – michalsol Jun 20 '17 at 11:09

1 Answers1

0

You can use a callback in the Fragment and the Activity stores your object.

Activity

public class MyActivity extends Activity implements MyActivityCallback{

    private Object myObject; // Replace with your object type

    @Override 
    public Object getMyObject(){
         return myObject;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myObject = new Object();
        // ...
    }

    // ....
}

MyActivityCallback

public interface MyActivityCallback{
   Object getMyObject();
}

Fragment

private MyActivityCallback mCallback;

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {
        mCallback = (MyActivityCallback) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString()
                + " must implement " + MyActivityCallback.class.getSimpleName());
    }
}

@Override
public void onDetach() {
    mCallback = null;
    super.onDetach();
}

Then in your Fragment, to access your object you do mCallback.getMyObject();

Eselfar
  • 3,759
  • 3
  • 23
  • 43
  • sorry, but this doesnt answer my question. I have 3 fragments and what i want to get rid of is a NullPointerException. The problem is with the flow of execution. first value from fragment A comes. at this point of time, value from C is null. I will post the code . please do check – Abdul Kader Jun 20 '17 at 08:39
  • The problem to put your object in a Bundle to send it to the next Fragment is that it needs to be Parcelable which is a pain in the a** to implement and maintain. Using the Activity to store the data (or an external data manager) is easier. But I'll have a look at your code when you'll update your post. :) – Eselfar Jun 20 '17 at 08:44
  • Maybe you should update the title as it seems that you're looking for a solution to pass data in between Fragments when, in fact, you try to solve a NullPointerException in your current implementation. – Eselfar Jun 20 '17 at 08:56