user3243651
user3243651

Reputation: 241

Restart application when i click a button

I need that my application restarts when click a button, for more info I need simulate thant I push back key and open application again. I supose that i can close application with finish(); but how can i do to launch onCrete() again? is there other way to do this?

thanks

Upvotes: 2

Views: 2453

Answers (3)

Atul O Holic
Atul O Holic

Reputation: 6792

Call your Activity using an Intent. It will launch the activity again.

Intent intent = new Intent(MyActivity.this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this will always start your activity as a new task
startActivity(intent);

You can also add, android:launchMode="singleTask" inside your Manifest so that only one instance of your activity will be maintained at any time.

Same thing here. :)

Upvotes: 2

Strigger
Strigger

Reputation: 1903

To restart your application you can use alarm manager to make your activity open after you exit the app.

AlarmManager alm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alm.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0));
android.os.Process.sendSignal(android.os.Process.myPid(), android.os.Process.SIGNAL_KILL);

Upvotes: 0

Shriram
Shriram

Reputation: 4411

Intent i = getBaseContext().getPackageManager()
             .getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

Upvotes: 0

Related Questions