Hello,
Native Android does not provide this API in Fragment
, so you need to use the interface to pass methods from Activity
to Fragment
.
Using Fragments Walkthrough as an example, you can refer to the following steps:
Step 1: Create an interface:
public interface UserInteractionListener
{
void onUserInteraction();
}
Step 2: Create an instance variable in your activity:
private UserInteractionListener userInteractionListener;
public void SetUserInteractionListener(UserInteractionListener userInteractionListener)
{
this.userInteractionListener = userInteractionListener;
}
Step 3: Implement the interface in your Fragment:
class DetailsFragment : Fragment,UserInteractionListener
...
public void onUserInteraction()
{
//TODO://do your work on user interaction
Toast.MakeText(Context, "test", ToastLength.Short).Show();
}
Step 4: Invoke your activity's userinteraction setter method:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
...
((DetailsActivity)Activity).SetUserInteractionListener(this);
...
}
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.