I am working on a application which takes a user's mobile number and stores the number in a file locally and stores it in a cloud database as well . I have written application logic in such a way that if the user exits from the app it maintains the state. When user opens the App again, the home page is opened with the same mobile number. I want to clear the all the data on Logout button click, and then again want to open the MobileHarvestMainActivity.java
file (ask to enter the number again from user).
I tried the link How to programmatically clear application data — it worked but when using the intent to send the MainActivity page App crashes.
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();
}
void calling() {
Intent intent = new Intent(getApplicationContext(), MobileHarvestMainActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}