Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces. Interfaces are better in situations in which you do not have to inherit implementation from a base class.Interfaces are useful when you cannot use class inheritance.
Make an interface like given below where we define multiple methods with different parameters.
1 2 3 4 5 6 7 8 |
public interface OnCustomPasser { void customData(ArrayList<CustomoptionData> list); void getFileCode(String fileCode); void getChecklist(HashMap map); void clearCheckList(String id); void getRadioProductId(int id); void getSpinnerProductId(int id); } |
Any fragment that should pass data back to its containing activity should declare an interface to handle and pass the data.Then make sure your containing activity implements those interfaces. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class ABC extends Fragment implements OnCustomPasser{ @Override public void customData(ArrayList list) { customList = list; } @Override public void getFileCode(String fileCode) { fileCode = fileCode; } @Override public void getChecklist(HashMap map) { checklist = map; } @Override public void clearCheckList(String id) { checklist.remove(id); } @Override public void getRadioProductId(int id) { radioProductOptionValueId = id; } @Override public void getSpinnerProductId(int id) { productOptionValueId = id; } |
Here, ((Your Fragment which implements the interface )).getFileCode(Parameters which you want to be transferred) replace fragment with the container and commit. In your fragment where getFileCode you will get your data in your replace fragment.
1 2 3 4 5 6 |
Fragment mFragment = new MyFragment(); ((MyFragment)mFragment).getFileCode("FileA"); ((CheckoutActivity) mcontext).getSupportFragmentManager().beginTransaction() .replace(R.id.checkout_container, myFragment, "myFragment") .addToBackStack("paymentMethod") .commit(); |