0

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

Community
  • 1
  • 1
bastami82
  • 5,955
  • 7
  • 33
  • 44
  • possible duplicate of [Quitting an application - is that frowned upon?](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon) – Machado Jul 14 '15 at 18:00
  • what about if you reorder your activities stack on that button click to remover all activities and go the main one and there just call finish(). maybe using FLAG_ACTIVITY_CLEAR_TOP. – Tobiel Jul 14 '15 at 18:11
  • 2
    "Then what method a banking App uses to terminate the app instantly by a click?" -- first, please explain, in precise technical terms, what you mean by "terminate the app". Then, please explain, in precise technical terms, how you have determined that "a banking App" does something to "terminate the app". – CommonsWare Jul 14 '15 at 18:21
  • @ Machado the Post that you referring is in paragraph number 2 (the reason to not use has a referral to that post ) – bastami82 Jul 14 '15 at 18:32
  • @Tobiel I tried this with only one activity too but it does not work by using finish() to close that activity I expect to see a fresh state when I return or re open the app but what I see is the previous state of that activity. – bastami82 Jul 14 '15 at 18:38
  • @CommonsWare , first thanks for time to respond, What I meant by 'Terminating the app' is that the app won't back to the previous state of closed activity. app will run fresh next time you run it. sort of after effect of of System.exit(0); in nutshell a new start for app. I do not know the "precise technical terms" but what I like to know is how banking App behave that way (when you exit, or leave the activity, you need to start the app again. hope I answered your question and I hope you understand not everyone here is in the same level of android knowledge hence stackoverflow. – bastami82 Jul 14 '15 at 18:52
  • start an activity in new_task | clear_task mode. Then finish it. – njzk2 Jul 14 '15 at 18:54
  • 1
    "the app won't back to the previous state of closed activity" -- that is what `finish()` does. "finish() does not do the same job for me" -- then you need to explain, in detail, what "the same job" is and what `finish()` is not doing. You might also want to explain what "user leave the main screen" means. "I hope you understand not everyone here is in the same level of android knowledge" -- I hope you understand that you have to provide [a minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) of what is not working for you. – CommonsWare Jul 14 '15 at 19:04
  • @njzk2 could you explain your solution a bit more (I am learning android) and I think I read similar concept solution with your answer somewhere else, it was to open another activity (in order to send the main activity to background) and then run finish on main activity). but what I have is only one activity in my test app). – bastami82 Jul 15 '15 at 11:21
  • @CommonsWare I added more details to my post as you requested. please revisit the question description. – bastami82 Jul 15 '15 at 11:41
  • 1
    "when I click on app icon again I see all previous data in the list view" -- if you have called `finish()`, you are looking at a new activity instance when you "click on app icon again". This means that wherever "all previous data" is coming from, it is outside the scope of the activity, such as in a static data member. You are going to need to identify where this data is coming from and take steps to not use that data when you do not want to. – CommonsWare Jul 15 '15 at 11:48
  • THANKS and +1 for mentioning static members. As you pointed I noticed that the reason my app is not starting fresh is that I used a static variable to save retrieved data.( I changed that and it works . After all it seems finish does the job! thank you. I leave this question open cause I am still interested to know how banking app does log in / log out. – bastami82 Jul 15 '15 at 14:28

2 Answers2

0

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.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • LOL your post made me laugh.. , I saw that response too in other forum. I will try your solution and let you know tomorrow. I am heading out for 3d Terminator 3 ... cheers – bastami82 Jul 14 '15 at 19:04
  • Just so you know, the static references to Activities was in the same code as this one: https://www.reddit.com/r/programminghorror/comments/3c85pp/read_stream_as_text/ <-- just to prove just how bad it is – EpicPandaForce Jul 14 '15 at 19:08
  • thanks again for reply, I am not sure if I follow ... I just updated the question description and I will be grateful if you let me know how to implement you solution in my case. – bastami82 Jul 15 '15 at 11:50
0

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?

bastami82
  • 5,955
  • 7
  • 33
  • 44