How do I programmatically 'restart' an Android app

How do I programmatically 'restart' an Android app

Sometimes we need to restart our Android app between the code execution, In that case, we have to use some set of code to perform this action. Now we will learn How do I programmatically restart an Android app?.

So, simply use the PendingIntent to do this activity with help of AlarmManager.

PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

Use this in your code like this:

try{
   Intent mStartActivity = new Intent(context, SplashScreenActivity.class);
   int mPendingIntentId = 123456;
   PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
   AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
   mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
   System.exit(0);
}catch (Exception e){
   e.printStackTrace();
}

Try to use the above code within the try block.

Related posts

Write a comment