Detect user interaction in fragments

V, Kavyasri 41 Reputation points
2022-10-28T05:16:37.38+00:00

In our app,we are using session management, the problem is even when the user is interacting with the dialog fragment,it is not extending the session, Is there any way to detect user interaction in dialog fragment like we have default method (OnUserInteraction()) in an Activity?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,013 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 16,621 Reputation points Microsoft Vendor
    2022-10-31T03:11:17.26+00:00

    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.