Programatically restart app on Android - MAUI

vanKraster 201 Reputation points
2024-07-25T07:49:04.6866667+00:00

Hi to all,

As per title I want to programatically restart the app and I am using the below code.
The problem is that it is not restarting the app, it is somehow closing it but it remains in background and it does not appear automatically I have to open all applications in background ( I see it ) and then I tap on it to reopen.
I need to restart the app, close and then reopen.
Thanks.

 public void RestartApp(string parameter)
 { 
     var activity = Platform.CurrentActivity;
     var packageManager = activity.PackageManager;
     var intent = packageManager.GetLaunchIntentForPackage(activity.PackageName);
     intent.AddFlags(ActivityFlags.ClearTop);
     intent.AddFlags(ActivityFlags.NewTask);
     var componentName = intent.Component;
     var mainIntent = Intent.MakeRestartActivityTask(componentName);
     var pendingIntent = PendingIntent.GetActivity(activity, 0, mainIntent, PendingIntentFlags.Immutable);
     var alarmManager = (AlarmManager)activity.GetSystemService(Context.AlarmService);
     alarmManager.Set(AlarmType.Rtc, Java.Lang.JavaSystem.CurrentTimeMillis() + 1000, pendingIntent);
     activity.FinishAffinity();
     Java.Lang.JavaSystem.Exit(0);
 }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,915 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 79,701 Reputation points Microsoft Vendor
    2024-07-26T02:37:04.9733333+00:00

    Hello,

    Firstly, please open your AndroidManifest.xml file and add following permissions in the <manifest>.

      <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" android:maxSdkVersion="32"/>
    <uses-permission android:name="android.permission.USE_EXACT_ALARM" />
    

    Then, you can use following code to restart your application.

    #if ANDROID   
       var context = Platform.AppContext;
          PackageManager packageManager = context.PackageManager;
          Intent intent = packageManager.GetLaunchIntentForPackage(context.PackageName);
          ComponentName componentName = intent.Component;
          Intent mainIntent = Intent.MakeRestartActivityTask(componentName);
          mainIntent.SetPackage(context.PackageName);
          context.StartActivity(mainIntent);
          Runtime.GetRuntime().Exit(0);
    
    #endif
    

    And donot forget to add following namespace.

    #if ANDROID
    using Android.App;
    using Android.Content;
    using Android.Content.PM;
    using Java.Lang;
    #endif
    

    As note: Please do not test this code in the debug mode, you can install application in the debug mode. Then stop the debug in VS. You can find your application in the emulator or device, open it manually and test this application restart code.

    Best Regards,

    Leon Lu


    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.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.