Search code examples
androidandroid-activityactivity-finish

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


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 https://stackoverflow.com/a/24015569/4173238 )

  1. some say use System.exit(0); according to this https://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


Solution

  • 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?