by "restarting" the application, what I am essentially attempting to do is reset all the instance variables, fields, activities, services, and just everything the application to how it was when it was first opened. The only caveat is that I don't want to actually exit out of the app itself. Is there any way I would be accomplish this?
-
Local variables? – shmosel Jan 29 '19 at 01:52
-
@shmosel sorry, meant instance variables – Solaire Jan 29 '19 at 01:59
-
https://stackoverflow.com/questions/15564614/how-to-restart-an-android-application-programmatically and https://stackoverflow.com/questions/2470870/force-application-to-restart-on-first-activity – Andreas Jan 29 '19 at 02:00
-
@Andreas yeah I went through basically every question (including those ones) that had to do with restarting and they all close the application. I just want to reset all the variables so its *as if it was restarted*, but I don't actually want the application to close and then open up again. – Solaire Jan 29 '19 at 02:05
-
then how about setting default values for those variables and reassigning it back? – Andreas Jan 29 '19 at 02:08
2 Answers
There's really no quick and easy solution to reset everything.
The main reason is because there's no way of knowing everything you want to reset. For example, there's a simple way to reset an Activity by relaunching the Activity and clearing the old one... however, this only resets the Activity minus the static variables. It also doesn't touch Services, Singletons, and other static values you've changed.
Only proper and reliable solution is to create a resetAll() method yourself. Have it send the reset commands to every active Service, change back default values for current Activity, null the Singletons, and etc.

- 3,184
- 1
- 11
- 12
What I did before on android (iOS doesn't allow to restart an app) is use a PendingIntent
with a code for clearing the cache of the app. It still exits the app but it restarts it like it's from a fresh install.
cleanCache();
PackageManager pm = context.getPackageManager();
if(pm != null){
Intent activity = pm.getLaunchIntentForPackage(getBaseContext().getPackageName());
if(activity != null){
activity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int pendingIntentId = 223344;
PendingIntent pendingIntent = PendingIntent.getActivity(context, pendingIntentId, activity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 5, pendingIntent);
System.exit(0);
}else{
Log.d(TAG, "was not able to restart, activity null");
}
}else{
Log.d(TAG, "was not able to restart, pm null");
}
public class ExitActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exit);
android.os.Process.killProcess(android.os.Process.myPid());
}
}
Then below are the code for cleanCache().
private void cleanCache() {
clearApplicationData();
Intent intent = new Intent(context, ExitActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(intent);
}
public void clearApplicationData(){
File cache = context.getCacheDir();
File appDir = new File(cache.getParent());
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");
webMain.clearCache(true);
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}
public static boolean deleteDir(File dir){
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
Then below is the code for ExitActivity.
public class ExitActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exit);
android.os.Process.killProcess(android.os.Process.myPid());
}
}

- 342
- 3
- 23