0

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);

}

}

Community
  • 1
  • 1
Alpesh
  • 265
  • 2
  • 7
  • 22
  • You're accessing the first index of an array or list that has no elements in it (which probably means its a list). Either ensure it always has something in it, or check the size before trying to call get on it. – Gabe Sechan Apr 14 '13 at 16:39
  • The code for logout was invisible - enclose text in `` inside back-quotes ``like `` backquotes`` — or, more relevantly, put it into an edit of the question, not in a comment. – Jonathan Leffler Apr 14 '13 at 16:51
  • Sir, i have edited the code plz help me now..thanks for your co-operation – Alpesh Apr 14 '13 at 17:23
  • I got the solution .. the clearing cache is working properly. problem was with my main Activity...thanks – Alpesh Apr 22 '13 at 04:54
  • The Link is very usefull for the same kind of thinghttp://stackoverflow.com/questions/6134103/clear-applications-data-programatically – Alpesh Apr 26 '13 at 12:30

0 Answers0