Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear app data programmatically but not force close android java

how to clear app data programmatically

I know we can clear data in the mobile device through:

Settings->Applications-> ManageApplications-> My_application->Clear Data

i want to implement this method when clicked on LOGOUT button in my application. But it force closes the app. Instead I want to start the main activity where the signup/sign in form is present.. How will this be possible?

like image 851
Dimas Fendriansyah Avatar asked Jan 26 '26 12:01

Dimas Fendriansyah


1 Answers

  if (VERSION_CODES.KITKAT <= VERSION.SDK_INT) {
        ((ActivityManager) context.getSystemService(ACTIVITY_SERVICE))
                .clearApplicationUserData(); // note: it has a return value!
    } else {
        try {
            // clearing app data
            Runtime runtime = Runtime.getRuntime();
            runtime.exec("pm clear YOUR_APP_PACKAGE_GOES HERE");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
like image 156
Behnam Nasehi Avatar answered Jan 28 '26 02:01

Behnam Nasehi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!