有时我们想刷新一个活动,但无法刷新。在那个地方,如果我们重新启动我们的应用程序,那么它会自动刷新。另外,我们可以使用它在崩溃时重新启动我们的应用程序。大多数时候,当我们打开任何应用程序时,它都会获取当前可用的所有数据。但是,如果与此同时,如果有更多数据更新到数据库,那么我们可以使用此功能重新启动以在每次获取新数据时再次获取所有数据。
现在,这里的要点是我们如何在Button Click上以编程方式重新启动Android应用程序。因此,在本文中,我们将讨论三种不同的方法来通过按钮单击以编程方式重新启动Android应用程序。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
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();
}
});
}
}
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());
}
});
}
}
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());
}
});
}
}
方法1
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
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();
}
});
}
}
输出:
方法二
将此实现添加到您的构建中。 gradle文件
implementation ‘com.jakewharton:process-phoenix:2.0.0’
在Androidmanifest.xml文件中添加此默认值
将以下代码添加到MainActivity。 Java文件
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());
}
});
}
}
输出:
方法3
将以下代码添加到MainActivity。 Java文件
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());
}
});
}
}
输出: