Different Ways to Programmatically Restart an Android App on Button Click - GeeksforGeeks

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 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>
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:
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:
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:
Android-Misc
Technical Scripter 2020
Android
Java
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Retrofit with Kotlin Coroutine in Android
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Android Listview in Java with Example
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples