1

I developed an application, especially for Exam, this app disallows students to use the back button (will clear its data and cache) and the Recent App button also disabled, but this app has a weakness: Students still can open another application (e.g Browsing) when they click the Home button.

I tried to do like this in the onStop method

@Override
protected void onStop(){
    super.onStop();
    this.finish();
    clearApplicationData();
}

And this is the code of the clearApplicationData method :

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("ERROR", " File /data/data/APP_PACKAGE/" + s + "");
            }
        }
    }
}

But it's not working, the app can still be opened and the data isn't cleared Can you tell me what's wrong?

  • Have you tried overriding `onPause` instead of `onStop`? See https://stackoverflow.com/a/11387395/19224040 – Rob Mar 15 '23 at 07:09

2 Answers2

0

First of all, you want to be aware of the android lifecycle you can know more about per android lifecycle when the user clicks the home button and then call the onPause() method.

  • You can do like below

     @Override
     protected void onPause() {
     super.onPause();
     clearApplicationData();
     finishAffinity(); 
    }
    

finishAffinity() This method kills all process of the application and then reopen the application to start with a new process

jcredking
  • 314
  • 1
  • 10
0

way 1.

getActivity().finish();
System.exit(0);

way 2. API 16 -> new

this.finishAffinity();

way 3. API 21 -> new

finishAndRemoveTask();
Javatar
  • 131
  • 6
  • I tried but the code is not supported, finishAffinity and finishAndRemoveTask showing red line below. but System.exit is worked, thank you Javatar.. – Terizla Smith Mar 15 '23 at 12:45