How to completely restart the application?

I have an application that starts a remote service in the first running action. Then, in another action, the user can configure the application. Please note that this second action is not related to the Service, and I do not want to associate it.

Now my question is: how could I restart the entire application from the second action after changing the configuration settings?

I am currently using a button that has onClickListener:

public void onClick(DialogInterface dialog, int which) { sauvegarde(); Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName()); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); } 

The problem is that it restarts the current activity only after disconnecting the entire application and, therefore, without restarting the service

Any ideas?

+8
source share
7 answers

You can use the Androids AlarmManager system as follows:

Code to restart the application in your activity:

 AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(this.getBaseContext(), 0, new Intent(getIntent()), getIntent().getFlags())); System.exit(2); 

An example can be found here.

UPDATE

As @CommonsWare pointed out, this is a bad way to develop your application when you need to restart it (bad practice). If you really want to do this, you can try to set an alarm to start your application a second after you kill your own process:

 AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(this.getBaseContext(), 0, new Intent(getIntent()), getIntent().getFlags())); android.os.Process.killProcess(android.os.Process.myPid()); 
+9
source

try it

  public void reload() { Intent intent = getIntent(); overridePendingTransition(0, 0); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); finish(); overridePendingTransition(0, 0); startActivity(intent); } 
+1
source

AlarmManager solution cannot work, because when you start the application, all pending intentions can be deleted. On android-10, I have this problem.

But I found another way - I create an Instrumentation class and call startInstrumentation on Context . Worked very well.

boolean android.content.Context.startInstrumentation (computer_name class name, String profileFile, Bundle arguments)

Begin executing the android.app.Instrumentation class. This Instrumentation Component will be launched by killing its target application (if it is currently running), starting with the target process, creating an instance of the instrumentation component, and then allowing it to control the application.

This function is not synchronous - it is returned as soon as the toolkit has begun and while it is working.

Instrumentation is usually allowed to be run only with a package that is either unsigned or signed that the instrument package is also signed (providing the target trusts the instrumentation).

Instrumentation must be defined in the manifest. Google how to use it.

+1
source

This is the latest answer with the latest updated testing of 2017 and working like a charm that will help :)

  Intent i = getBaseContext(). getPackageManager(). getLaunchIntentForPackage(getBaseContext().getPackageName()); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//clear the stack i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); finish();//don`t forget to finish before starting again startActivity(i);//start the activity again 
+1
source

try this code

 public void onClick(DialogInterface dialog, int which) { sauvegarde(); restart(2); } 

reboot method

 public void restart(int delay) { Intent launchIntent = new Intent(context, YourStartUpActivity.class); PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0, launchIntent , 0); AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); manager.set(AlarmManager.RTC, System.currentTimeMillis() + delay, intent); System.exit(2); } 
0
source

in your reload activity add this

 restart.this.finish(); getActivity(otheractivity.class).finish(); Intent in = new Intent(this,main.class); startActivity(in); 

for each of your actions add getactivity (yourclassname) and then complete the action. if you add all your actions like this, your application will close and restart

0
source

Using the pending intent, you can restart your application whenever you want. Just use the lines of code below.

 int pendingId_Intent = 987654; Intent YourMainActivityIntent = new Intent(Context, YourMainActivity.class); PendingIntent intentPending = PendingIntent.getActivity(context, pendingId_Intent, YourMainActivityIntent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager alarm_manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); alarm_manager.set(AlarmManager.RTC, System.currentTimeMillis() + 100, intentPending); System.exit(0); 
0
source

Source: https://habr.com/ru/post/948052/


All Articles