closing an activity using finish(); wont make app start fresh in next run! why?

| | August 4, 2015

I wondered if anyone can shed some light on this,

I have one activity app which has a listview. In onCreate() I populate some data in that listview (reading it from sqlite database). I have a close button which triggers finish(); too. now when I press close button, activity disappears but when I click on app icon on desktop (or selecting it from phone history button) I see all previous data in the listview. the function that I am looking for is to start app fresh after clicking close button for next run. (something like banking app log-out button). (list view here is only an example to put across the need, so clearing list-view before finish(); is not acceptable. It is very simple and plain request and I do not think any code is necessary but if anyone interested I will post some code too.

What I want is same behavior as a banking app in exit, when user leave the main screen or click sign out, the App closes altogether. I can achieve this by using following methods (number 2 and 3) but apparently these solutions are not best practices. Then what method a banking App uses to close the app instantly? I doubt they use something which is not best practice such as System.exit(0)?! or do they!

  1. Many developers claiming closing an App or killing parent activity
    is OS job

  2. Some say use use :

    int pid = android.as.Process.myPid();

    android.os.Process.killProcess(pid);

(this solution according to this is not a good idea because in next run app acts like it has been crashed last time http://stackoverflow.com/a/24015569/4173238 )

  1. some say use System.exit(0); according to this
    http://stackoverflow.com/a/5846275/4173238 is not recommended either

  2. some say use finish(); but finish does not do what I want

Thanks for any input

2 Responses to “closing an activity using finish(); wont make app start fresh in next run! why?”

  1. As brilliant CommonsWare, has pointed out in his comment “Static” was the issue! I was using static variables to store data to fill My listView. Apparently even if you have only one Activity and close it, Static variables remain intact! on app re run!
    If you asking why I used static variable at the first place, I have to say, right or wrong, I wanted to share that variable between my other java class (my databaseHandler.class).

    Why Android not clear all (including static variables) resources when closing the main and only Activity of app, remains a question and this is my next reading topic! but many thanks for anyone who post a comment on this question,

    I will also change the question from:

    How Banking Apps close? finish() does not do the same job for me

    to

    closing an activity using finish(); wont make app start fresh in next
    run! why?

  2. EpicPandaForce on November 30, -0001 @ 12:00 AM

    If you have a mechanism that allows it so that you only deliver messages to the main thread when the application is resumed, then you can register your activities on an event bus like Otto and send an event that every Activity is subscribed to on which they call finish() on themselves.

    Another possible solution is starting every activity with startActivityForResult(), and if you click the exit button, then you would say

        public static final int KILL_ACTIVITY_RESULT_CODE = 0xD34DB33F; //why not
    
        public boolean onOptionsMenuItemSelected(MenuItem menuItem) {
              if(menuItem.getId() == R.menu.kill_activity) {
                   setResult(KILL_ACTIVITY_RESULT_CODE);
                   finish();
              }
        }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(resultCode == KILL_ACTIVITY_RESULT_CODE) {
                setResult(KILL_ACTIVITY_RESULT_CODE);
                finish();
            }
        }
    }
    

    …and one time I’ve seen someone make static references to every single activity they had, and called finish() on each and every one of them. But don’t do that, that essentially means you have failed as an Android programmer, and there is nothing to redeem you of your sins.

Leave a Reply