简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM