Different Ways to Programmatically Restart an Android App on Button Click
Last Updated :
26 Feb, 2021
Sometimes we want to refresh an Activity, but there it is not possible to refresh. In that place, if we restart our app then it automatically gets refreshed. Also, we can use this to restart our app whenever it crashes. Most of the time when we open any app then it fetches all the data currently available. But if in the meantime if more data is updated to the database then we can use this feature to restart to again fetch all data every time it gets new data.
Now the point that comes here is how we can Programmatically Restart an Android App on Button Click. So in this article, we are going to discuss three different methods to Programmatically Restart an Android App on Button Click.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click on Button to restart the app"
android:textStyle="bold" />
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/black"
android:padding="10dp"
android:text="Click to Restart" />
</LinearLayout>
Method 1
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// after on CLick we are using finish to close and then just after that
// we are calling startactivity(getIntent()) to open our application
finish();
startActivity(getIntent());
// this basically provides animation
overridePendingTransition(0, 0);
String time = System.currentTimeMillis() + "";
// Showing a toast message at the time when we are capturing screenshot
Toast.makeText(MainActivity.this, "Current time in millisecond after app restart" + time, Toast.LENGTH_SHORT).show();
}
});
}
}
Output:
Method 2
Add this implementation to your build. gradle file
implementation 'com.jakewharton:process-phoenix:2.0.0'
Add this default value in the Androidmanifest.xml file
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Add the following code to the MainActivity.java file
Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.jakewharton.processphoenix.ProcessPhoenix;
public class MainActivity extends AppCompatActivity {
Button click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// this process phoenix library is used
// in case to restart our application
ProcessPhoenix.triggerRebirth(getApplicationContext());
}
});
}
}
Output:
Method 3
Add the following code to the MainActivity.java file
Java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Navigating From MainActivity to MainActivity.
// Navigate from this activity to the activity
// specified by upIntent,
// basically finishing this activity in the process.
navigateUpTo(new Intent(MainActivity.this, MainActivity.class));
startActivity(getIntent());
}
});
}
}
Output:
Similar Reads
How to Change the Screen Orientation Programmatically using a Button in Android? Generally, the screen orientation of any application is Portrait styled. But when it comes to gaming or any other multimedia service such as watching a video, the screen orientation must change functionally from Portrait to landscape or vice-versa when the functionality is not required. So a develop
3 min read
How to Quit Android Application Programmatically? When we want to implement an exit AlertDialog in our android application we have to programmatically exit our android application. In this article, we will take a look at How to Quit the Android application programmatically. We will be adding a button and on clicking on that button we will be closin
3 min read
How to Change App Icon of Android Programmatically in Android? In this article, we are going to learn how to change the App Icon of an App on the Button Click. This feature can be used when we have an app for different types of users. Then as per the user type, we can change the App Icon Dynamically. Step by Step Implementation Step 1: Create a New Project To
3 min read
How to Check Airplane Mode State in Android Programmatically? Airplane Mode is often seen in action during flights, avoiding calls, or rebooting the network on mobiles, tablets, and laptops. Airplane mode is a standalone mode where the device turns down the radio communications. These may include Wifi, GPS, Telephone Network, Hotspot depending upon the year of
3 min read
Android Jetpack Compose - Change the Screen Orientation Programmatically using a Button Many times in android applications we have to programmatically change the screen orientation to perform some task. If we want to play a video in landscape mode then we have to switch to landscape mode within our android application. So we have to change screen orientation in our android application
4 min read
How to Close Alert Dialog Box in Android Programmatically? AlertDialog is a flash or an alert message with options that let the user proceed or deny any process or action. AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be pr
2 min read
How to Listen for Volume Button and Back Key Events Programmatically in Android? By production, Android devices are provided with specific physical keys, such as Volume keys, Power key, Back key, Home key, and Activities key. These keys respond to a press. The same keys have particular functionality on the nature of the press. The volume key on a Single press increases or decrea
3 min read
How to Check the Battery Level in Android Programmatically? Sometimes, it is useful to determine the current battery level. One may choose to reduce the rate of your background updates if the battery charge is below a certain level. But one cannot continuously monitor the battery state. In general, the impact of constantly monitoring the battery level has a
2 min read
How to Change ActionBar Title Programmatically in Android? Whenever we develop an Android application and run it, we often observe that the application comes with an ActionBar with the name of the application in it. This happens by default unless explicitly changed. This text is called the title in the application. One can change the title of the applicatio
3 min read
How to Adjust the Volume of Android Phone Programmatically from the App? As stated in the title of this article let's discuss how to adjust the volume of an Android Phone programmatically from the App. Basically, control the volume in the app mean Increase or Decrease the Volume without the Volume Bar UIIncrease or Decrease the Volume with the Volume Bar UIMute or Unmut
4 min read