سؤال

I'm trying to implement returning from an Activity without needing to refresh the screen. As far as I see, there are 4 possible states to handle:

  1. Both "Use wireless networks" and "Use GPS satellites" are disabled (user is directed to My Location preferences and not allowed to continue if either are are disabled)
  2. One of these two are enabled (1 of 2 possibilities) 3, Both are enabled

My problem is that after returning from other Activities in my app, setupWebView() is called every time causing a screen refresh delay.

The refresh is somewhat necessary returning from the preferences (e.g. GPS after enabling gets a fix), but certainly not after returning from an Activity as the time delay in re-painting the screen is distracting and slows down the workflow of my app.

Could someone please advise how to handle this within my onResume() so to avoid all the setupWebview calls.

Here is the code from my onResume() within my main Activity:

    @Override
protected void onResume() {
    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            && !locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER))
        createMyLocationDisabledAlert();

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 500, 0, this);
        mostRecentLocation = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (mostRecentLocation != null)
            setupWebView();
        else {
            mostRecentLocation = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            setupWebView();
        }
    }

    else if (locationManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 500, 0, this);
        mostRecentLocation = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (mostRecentLocation != null)
            setupWebView();
        else {
            mostRecentLocation = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            setupWebView();
        }
    }

    // locationManager.requestLocationUpdates(provider, 500, 0, this);
    // mostRecentLocation = locationManager.getLastKnownLocation(provider);
    // } else
    // mostRecentLocation = locationManager
    // .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    super.onResume();
}
هل كانت مفيدة؟

المحلول

Use a boolean flag and use it to skip the call to setupWebView() -- and perhaps most of the code in your onResume() method -- in cases where you know it is safe to skip it. You are the one who is calling startActivity() to go to the location preferences screen, and hence you are the one who knows whether or not you need to call setupWebView().

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