Different Ways to Programmatically Restart an Android App on Button Click
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
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) {
finish();
startActivity(getIntent());
overridePendingTransition( 0 , 0 );
String time = System.currentTimeMillis() + "" ;
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) {
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) {
navigateUpTo( new Intent(MainActivity. this , MainActivity. class ));
startActivity(getIntent());
}
});
}
}
|
Output:
Last Updated :
26 Feb, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...