[英]How to restart app and clear all data with Button
如果你去手机上的程序信息,你会看到删除所有数据。 问题是如何在 Java 中的 MainActivity 中执行此操作并重新启动它。 甚至有可能这样做吗?
我需要这个才能退出。 在我的项目 (MainActivity) 中,有一个通过 Google 和电子邮件和密码登录的帐户。 当我调用 signOut() 函数时,我没有注销我的帐户。 为了让生活更轻松,我决定清理并重新启动。
Button btnClearAndRestart = findViewById(R.id.btn);
btnClearAndRestart.setOnClickListener(new View.OnClickListener(){
//Delete all app data and restart app
});
public boolean clearApplicationUserData()
返回:如果应用程序成功请求擦除应用程序的数据,则为真; 否则为假。
在申请被关闭之前,我们有一个返回者。 所以,我们将使用这个返回者来重新启动应用程序。
if(ActivityManager.clearApplicationUserData)
{
doRestart = true;
}
当 Activity onDestroy() 和 onStop() 被称为重启应用程序时。
@Override
protected void onDestroy() {
super.onDestroy();
if(doRestart){
Intent intent = new Intent(this, Activity.class);
this.startActivity(intent);
}
}
@Override
protected void onStop() {
super.onStop();
if(doRestart){
Intent intent = new Intent(this, Activity.class);
this.startActivity(intent);
}
}
我们在 onDestroy() 和 onStop() 中都放置了重新启动操作,以确保应用程序将再次重新启动。
而且,我认为在操作系统停止活动之前强制停止活动是个好主意。
if(ActivityManager.clearApplicationUserData)
{
doRestart = true;
finish(); <= i mean this
}
这是因为,它确保 onDestroy() 和 onStop() 将被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.