3

I want a way to restart an app after the user has made any in-app purchase that removes ad banners, so that the onCreate() methods gets called again.

The problem is, I don’t want any services to be killed, I just want the app to restart with changed restrictions

I found this code on Stack Overflow:

Intent mStartActivity = new Intent(getApplicationContext(), MainActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
ib.
  • 27,830
  • 11
  • 80
  • 100
User104163
  • 532
  • 6
  • 17
  • If you accept answer, please check it to close – Francesco Bocci Oct 07 '20 at 13:24
  • Since Android 10 you can't launch from background. use my method below. Use this: https://stackoverflow.com/a/71392776/2603965 – grebulon Mar 08 '22 at 09:41
  • Does this answer your question? [Restarting Android app programmatically](https://stackoverflow.com/questions/46070938/restarting-android-app-programmatically) – grebulon Mar 08 '22 at 09:41

2 Answers2

6

Update: To restart your application, Make your Root Activity's Intent, while setting these flags

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

This will ensure a fresh instance of your root activity is launched (the first flag), while all previous activities are popped off the stack (the second flag)


Use recreate() when inside the same activity.

If your app is not running, it's already refreshed on opening anyways.

Arvind
  • 724
  • 5
  • 6
3

Try this:

Intent refresh= new Intent(getActivity(), MainActivity.class);
startActivity(refresh);
Francesco Bocci
  • 827
  • 8
  • 19
  • This is great but is there a way to make it wait 1 or two seconds before it restarts? I want the app to have finished writing a boolean to sharedpreferences before it starts again (the boolean is to state that a purchase was made) – User104163 Oct 07 '20 at 13:26
  • if you put this code after sharedpreferences, it will wait that all is finish (is usless "wait 1 or 2 seconds") – Francesco Bocci Oct 07 '20 at 13:28
  • Hi are you certain that editor.apply() will always finish before the code under it? – User104163 Oct 07 '20 at 13:39
  • You can check if editor.apply() was successful. But using timer is not a good idea – Francesco Bocci Oct 07 '20 at 13:43
  • Also what if the user returns to a previous activity after restarting this one? The previous activity is still using the state that it was created in earlier, meaning it still has the ad banner. Isn't better to restart the whole app rather than just the activity the user is currently in? – User104163 Oct 07 '20 at 13:47
  • This is not good solution man.. Where is your flags? This code keep activities in ram.. – Samir Alakbarov Dec 18 '20 at 10:28
  • This worked great for me, though it doesn't restart the app, just the activity. Note for future readers, getActivity doesn't work in Activities (according to s.o on SF), use getApplicationContext instead. – StellarEquilibrium Aug 03 '21 at 13:46