Hello,
Welcome to our Microsoft Q&A platform!
is there a global touch Event in Xamarin.form?
No, But Android have a DispatchTouchEvent
in the MainActivity.cs
.
We can write a SessionManager
like this thread: https://stackoverflow.com/a/51727021/10627299
Then, Use it in the MainActivity.cs
. When have a click, the task will run, after 30 seconds, HandleSessionExpired
will be execute.
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;
using Android.Views;
namespace DetectorNoAction.Droid
{
[Activity(Label = "DetectorNoAction", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnUserInteraction()
{
base.OnUserInteraction();
}
void HandleSessionExpired(object sender, EventArgs e)
{
App.Instance.DoLogoutAsync();
}
public override bool DispatchTouchEvent(MotionEvent ev)
{
switch (ev.Action)
{
case MotionEventActions.Up:
SessionManager.Instance.SessionDuration = TimeSpan.FromSeconds(30);
SessionManager.Instance.OnSessionExpired = HandleSessionExpired;
SessionManager.Instance.StartTrackSessionAsync();
break;
default:
SessionManager.Instance.EndTrackSession();
break;
}
return base.DispatchTouchEvent(ev);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
After execute App.Instance.DoLogoutAsync();
, it will open login page in the App.xaml.cs
public partial class App : Application
{
public static App Instance { get; set; }
public App()
{
InitializeComponent();
Instance = this;
MainPage = new MainPage();
}
public void DoLogoutAsync()
{
Console.WriteLine("========================");
MainPage = new LoginPage();
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
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.