I created an app using Xamarin.Forms. I am trying to detect if there is a change in phone's language each time the app is reopened after being closed using the back button. Tried to Debug.WriteLine
on each of onStart
, onSleep
and onResume
to see which one occurs when I open the app again but none of them worked. This is what i tried:
App.xaml.cs
protected override void OnStart()
{
Debug.WriteLine("onresume");
//CultureInfo language = CultureInfo.InstalledUICulture;
//Thread.CurrentThread.CurrentUICulture = language;
//ApplicationResource.Culture = language;
//Application.Current.MainPage = new NavigationPage(new MainPage());
}
protected override void OnSleep()
{
Debug.WriteLine("onsleep");
}
protected override void OnResume()
{
Debug.WriteLine("onresume");
}
How do I know when the app is reopened so I could try the language change code?
If i understand your concern correctly two things can help you:
SingleTask
for your mainactivty. Basically if you "close" the app using Back button and then click on app icon, the app is launched from start recreatingApp
but keeps static variables and the debugger is still connected too. You can detect it setting a static variable, then checking it with a breakpoint insideApp.cs
constructor. So the avoid such just useLaunchMode = LaunchMode.SingleTask
inside theMainActivity.cs
[Activity]
attribute. This way after you return to the app after the Back button + click on icon you'll get a fresh new app withOnStart
invoked.Some could say to use
LaunchMode.SingleInstance
but it will kill your app if you click on push notification whie app is active so better useSingleTask
, it will open smoothly the running app.