1

On start my app downloading json files. When the app enter in background after a home button click and resume an another day, the json datas are incorrect so the app close printing error.

I would like to exit app on home button click but I saw that it's not possible? How I can do to reload json data when app resume an another day?

user3264399
  • 284
  • 1
  • 13
romain
  • 15
  • 2
  • try to load your json in back ground thread – rajahsekar Feb 03 '14 at 08:23
  • Try the following answer to detect the `HOME` key pressed: http://stackoverflow.com/questions/2208912/how-can-i-detect-user-pressing-home-key-in-my-activity/17756786#17756786 . From there you can finish if you wish. – Shobhit Puri Feb 03 '14 at 08:28

2 Answers2

2

If i understand you correctly, you need to load the JSON data every new day, right?

Put a timestamp (millis) in the onStop-Lifecycle-Method and save the value in the prefs. When the app comes back to foreground onResume will be called. Make a new timestamp (millis) in onResume and compare the new timestamp with the stored timestamp in your prefs. You can compare the two timestamps with Date-class and SimpleDateFormat-class. You just need to compare the day-values. If those two values are not equal, it is a new day...go and catch new JSON data...

A.D.
  • 1,412
  • 2
  • 19
  • 37
  • I used "Arju" method because i wanted exit app but your answer it's realy good. To be honest i did your method before asking help here. – romain Feb 03 '14 at 09:04
1

You can actually manage homeButton event by android lifecycle. There is no direct click event available. Instead try Within onStop try like this

    @Override
protected void onStop() {
    super.onStop();
    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    // The first in the list of RunningTasks is always the foreground task.
    RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
    if(!foregroundTaskInfo.topActivity.getPackageName().equals(this.getPackageName()))
    {
        // The app is exiting no other activity of your app is brought to front
                    finish();
    }
}
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64