I'm trying to create a 'log out' function within my application. Basically, by logging out, the application data should be cleared. What I would like to do is after logging out, the application should restart so that credentials etc. can be entered again. The problem I'm having is that at the point of the user clicking 'log-out', the application already has 3-4 activities running, and I'm not sure how to step back through them. How do I (simulate?) a restart of the app?
This example demonstrates how do I programmatically “restart” an Android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
use 'System. exit(0);' when you really want to exit the app.
In order to restart your application when it crashed you should do the following : In the onCreate method, in your main activity initialize a PendingIntent member: Intent intent = PendingIntent. getActivity( YourApplication.
Checkout intent properties like no history , clear back stack etc ... Intent.setFlags
Intent mStartActivity = new Intent(HomeActivity.this, SplashScreen.class); int mPendingIntentId = 123456; PendingIntent mPendingIntent = PendingIntent.getActivity(HomeActivity.this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager mgr = (AlarmManager) HomeActivity.this.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); System.exit(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With