I have an Activity A. Activity A calls fragment frag1. frag1 calls fragment frag2. Lastly frag2 calls frag3.
When I click on a button in frag3, I want to call frag1, pass and parse an object from frag3 to frag1. I tried to do this with an object bundle sent from frag3 to frag1.
I see that there is a popBackStack() method. However, I am a little confused on how this would work. Is it safe to use this method?
I do not know how to do it. Thanks in advance.
Use Event Bus pattern for passing data(events) between fragments. Then work with you fragment stack in usual way.
There are several popular libraries that implement event bus. I personally prefer
use Local Broadcast for this.
write Broadcast receiver on fragment 1,
On apply button of fragment 3, broadcast your data , and then pop fragment 3
example-
private final BroadcastReceiver myLocalBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
;
Bundle bundle = intent.getExtras();
if (bundle != null) {
get data from bundle
}
};
on fragment 3(apply button):
Intent localBroadcastIntent = new Intent(Constant.MY_ACTION);
Bundle bundle = new Bundle();
bundle.put("your data);
localBroadcastIntent.putExtras(bundle);
LocalBroadcastManager myLocalBroadcastManager = LocalBroadcastManager.getInstance(getActivity());
myLocalBroadcastManager.sendBroadcast(localBroadcastIntent);
getActivity().getSupportFragmentManager().popBackStack();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With