Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'restart' an android application programmatically [duplicate]

Tags:

android

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?

like image 628
Ryan Avatar asked Mar 22 '13 07:03

Ryan


People also ask

How do I programmatically restart an android 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.

How do I close a programmatically app in Android?

use 'System. exit(0);' when you really want to exit the app.

How do you auto restart an android application after a crash or a force close error?

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.


1 Answers

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); 
like image 107
Code_Life Avatar answered Oct 01 '22 16:10

Code_Life