我正在开发一个应用程序,它获取用户的手机号码并将号码存储在本地文件中,并将其存储在云数据库中。我以这样一种方式编写了应用程序逻辑,即如果用户退出应用程序,它会保持状态。当用户再次打开应用程序时,使用相同的手机号码打开主页。我想清除单击注销按钮上的所有数据,然后再次打开MobileHarvestMainActivity.java
文件(要求用户再次输入数字)。
我尝试了如何以编程方式清除应用程序数据的链接——它有效,但是当使用意图发送 MainActivity 页面时应用程序崩溃。
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
Toast.makeText(getBaseContext(), "clicked", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ListeningAndSharingHomeActivity.this,MyActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
startActivity(intent);
finish();
}
});
}
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_my);
clearApplicationData();
}
public void clearApplicationData() {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
calling();
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
无效调用(){
Intent intent = new Intent(getApplicationContext(), MobileHarvestMainActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}