سؤال

My application calls a few web services when it is freshly launched. I would also like to call these web services if the user launches the app (which was running in the background) and resumes. I've looked into all the methods fired when apps are launched and when activities are in focus. onStart(), onResume(), onRestart() etc....

I thought onRestart would be a good bet to make my web service calls and update my view, problem is if I go from activity A (the one making the calls) to activity B and hit the back button, activity A will fire onRestart() and call the web services. I don't want this to happen everytime I go back to my main screen from some activity in my app. I only want the services to be called if my app activity A is brought into focus from the outside. Are their any other events I should be aware of that could help me? If I hit the home button and then hit my app icon it should update, but not if I click something on my main screen, opening a new activity and then hit the back button.

Hope the question makes sense.

Thanks.

هل كانت مفيدة؟

المحلول

According to the Activity lifecycle, onRestart will be called before onResume. So if you put your web calls in onResume, it will be called when the activity first starts and any time it resumes. onRestart is only called when the user navigates back to that activity. So you can have a boolean in your activity (ex. boolean isActivityRestarting) that is set to false, then set to true in onRestart. If it's true when onResume is hit, then don't do your web calls.

Example code:

public void onRestart() {
    isActivityRestarting = true;
}

public void onResume() {
    if (!isActivityRestarting) {
        executeWebCalls();
    }

    isActivityRestarting = false;
}

نصائح أخرى

Well onCreate is your best bet, but with some caveat. (main screen here is the screen which calls webservice)

  • If the user presses BACK button on the main screen, onCreate will be called.
  • If the user presses BACK button on the other screen and comes back to the main screend, onCreate wont be called.
  • If the user exits the main screen using HOME button, and comes back by long pressing HOME button and choosing your app, onCreate is not called.

So far so good, here is the catch.

  • If the user presses HOME button to exit and then goes to the launcher icon, the onCreate is not called. Theoretically it is consistent behavior but might look odd as user might be thinking that he/she is launching an app from start forgetting how he/she exited initially.

To your ans, onCreate is the way to go with slight checks.

Soln if using onStart or onRestart:

One check which you can do is store the time stamp in your Shared Preferences, which corresponds to when you called the web services. Now when user exited (whether by HOME or BACK key) and come back you can check the time and compare it against the stored time. If the difference is high just make the webservice call. To make this work you have to use onStart or onRestart.

Since this is normal behavior (see http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle) I would recommend not trying to work against it, but rather work with the lifecycle methods.

Your app is being brought to focus from the outside, whether from this other activity or otherwise.

Perhaps you should call the services in onCreate?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top