Home / Android F F P / Use and Listen call back into both activity and fragment from the same fragment

Use and Listen call back into both activity and fragment from the same fragment

Let’s say we have fragment which is used for login.

So we need to use this fragment wherever login action is necessary. We also need to get a callback from that fragment from where it created

We know that from an activity if we need to listen to call back from the fragment we need to implement an interface and use that interface in the fragment to trigger a callback to the activity.

but if we need to use this fragment in both activity and fragment we need to add a method in that fragment called setlistener(Callback)

whenever we open the fragment from other fragments we need to call that setlistener(this) method to initiate callback and implement that callback in the fragment where we expect to receive the callback.

and we need to change the on attach method a little bit like

Now it will return call back in wherever we want. In a activity or in a fragment.

For fragment, we only need to set the callback manually.

About Rana

Rana
Love to do fun with programming and build something new.. :)

Check Also

handle-new-notification-in-the-same-activity

How to refresh activity on new notification click – Android developer

In some case we face this kind of issue.. In a specific activity we are …

Leave a Reply

Your email address will not be published. Required fields are marked *