1

Alright, so basically, I want to be able to force my users to go through the first activity everytime they bring the app back up, so that it checks for updates. Since later versions of android prevent me from overriding the home button, I had relied on calling the onStop method to force the process to kill itself, forcing a reload when the app is opened again.

Problem is, now I cannot transition to new activities without the onStop method being called. Is there any other way to accomplish this, or to keep the onStop method from being called on activity transition?

Darek Kay
  • 15,827
  • 7
  • 64
  • 61
KGillis
  • 53
  • 7
  • 3
    You should reconsider your app logic. "Force my users" is really a bad idea. Why dont you check for your updates in some background thread or even better... service? – Okas Jul 14 '15 at 12:15
  • You shouldn't even consider this. You don't have to worry that users will only put your app in background, thus opening it back won't check for updates. Why? Because an app that is put in background won't remain running for much long. The Android OS will kill it. Thus, when the user opens it back, the update check will be made. At maximum, it will stay in background for a short period of time relative to the number of updates that you will probably make, so it is insignificant. – DDsix Jul 14 '15 at 12:27

2 Answers2

4

I'd avoid messing with the Activity Lifecycle at all cost. Also if you want your app to start from it's first activity every time, that's counter-intuitive to what Android users are used to ... which might cause you surprisingly more negative comments than expected.

If "checks for updates" is all you need to do, why don't just do it in onResume() in all your activities (hopefully you're already using some sort of a BaseActivity in your app that all others extend, so it's just gonna be 2 lines of code to add it)? If you need to update, than just redirect them to the update interface from there (or do whatever action you decide).

Vesko
  • 3,750
  • 2
  • 23
  • 29
  • To clarify a bit, this isn't a public app. It's for in-house use for warehouse workers. We are basically attempting to avoid the case where they keep the phone on indefinitely and the app sitting in the background indefinitely. – KGillis Jul 14 '15 at 12:22
  • 1
    Take a look at this page - can't imagine a better time to use Android for Work APIs: https://developer.android.com/training/enterprise/index.html. If you decide it's overkill to use those for your app, I'd fallback to rely on doing any checks in onResume() ... and from there you can easily redirect / close / whatever needed :) – Vesko Jul 14 '15 at 12:27
  • Unfortunately, onResume() is called when the activity is first created. I was able to exploit the onRestart() method, however. Thanks. – KGillis Jul 14 '15 at 13:02
1

If you want, you can catch the back button pressed.

You can for example start an Intent.

@Override
public void onBackPressed() {    
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    startActivity(intent);
}
phoenix
  • 7,988
  • 6
  • 39
  • 45
nekflamm
  • 131
  • 12
  • *If you want, you can catch the back button pressed* -> No, that's not what is asked, and it doesn't have the same effect, read the question carefully, we are talking about the home button, not the back button. – 2Dee Jul 14 '15 at 12:23
  • Thanks, but I already am catching the back button. – KGillis Jul 14 '15 at 12:25
  • excuse me i didn't understand. HomeWatcher will work – nekflamm Jul 14 '15 at 12:26