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();
}