Titanium JIRA Archive
Appcelerator Community (AC)

[AC-2857] Android: Map View state handling when app goes into background with 'Home' or 'Back' button is not opitmal

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionFixed
Resolution Date2011-08-01T03:45:57.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsandroid
ReporterJoe Chavez
AssigneeTony Guntharp
Created2011-06-25T09:01:57.000+0000
Updated2016-03-08T07:47:48.000+0000

Description

When an Android application that uses Map View as a UI component is exited by the user using the 'Home' or 'Back' button does not always resume properly when reopened by the user. In the worst case ('Back' button) the application is forced closed by the Android OS (user is given a notification dialog). In the best case ('Home' button) the map view resumes but is missing the 'blue' current location map indicator flag. I have coded a fix for this issue in the 1.7.0 SDK and will create a fork and push the changes in a few days. This problem only occurs on the device... not in the Android simulator. I am testing with an Samsung Infuse 4G.
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');


var isAndroid = false;

if (Titanium.Platform.name == 'android') {
    isAndroid = true;
}


if (Titanium.Geolocation.locationServicesEnabled===false) {
    Titanium.UI.createAlertDialog({
        title:'TC-105',
        message:'Please turn on location services.'
    }).show();
}

if (!isAndroid) {
    var authorization = Titanium.Geolocation.locationServicesAuthorization;
    Ti.API.info('Authorization: '+authorization);
    if (authorization == Titanium.Geolocation.AUTHORIZATION_DENIED) {
        Ti.UI.createAlertDialog({
            title:'TC=105',
            message:'You have disallowed TC-105 from running geolocation services.'
        }).show();
    } else if (authorization == Titanium.Geolocation.AUTHORIZATION_RESTRICTED) {
        Ti.UI.createAlertDialog({
            title:'TC-105',
            message:'Your system has disallowed TC-105 from running geolocation services.'
        }).show();
    }

}

Titanium.Geolocation.purpose = "TC-105 Threat Level";
Titanium.Geolocation.preferredProvider = Titanium.Geolocation.PROVIDER_GPS;

//
//  SET ACCURACY - THE FOLLOWING VALUES ARE SUPPORTED
//
// Titanium.Geolocation.ACCURACY_BEST
// Titanium.Geolocation.ACCURACY_NEAREST_TEN_METERS
// Titanium.Geolocation.ACCURACY_HUNDRED_METERS
// Titanium.Geolocation.ACCURACY_KILOMETER
// Titanium.Geolocation.ACCURACY_THREE_KILOMETERS
//
Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;

Titanium.Geolocation.distanceFilter = 50;



// create tab group
var tabGroup = Titanium.UI.createTabGroup();

//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({
    title:'Tab 1',
    backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({
    icon:'KS_nav_views.png',
    title:'Tab 1',
    window:win1
});

var label1 = Titanium.UI.createLabel({
    color:'#999',
    text:'I am Window 1',
    font: {
        fontSize:20,
        fontFamily:'Helvetica Neue'
    },
    textAlign:'center',
    width:'auto'
});

win1.add(label1);

//
// create controls tab and root window
//
var win2 = Titanium.UI.createWindow({
    url:'MapView.js',
    title:'Tab 2',
    backgroundColor:'#fff'
});
var tab2 = Titanium.UI.createTab({
    icon:'KS_nav_ui.png',
    title:'Tab 2',
    window:win2
});

//
//  add tabs
//
tabGroup.addTab(tab1);
tabGroup.addTab(tab2);

// open tab group
tabGroup.open();
var MapView = {
};

(function() {

    var mapView = null;
    var map = null;
    var isAndroid = false;

    function updateCurrentLocation(e) {
        Titanium.API.debug('updateCurrentLocation(e) - BEGIN');

        if (!e.success || e.error) {
            Ti.API.debug("Error: "+e.error);
        } else {
            Ti.API.debug("e.coords.latitude: " + e.coords.latitude);
            Ti.API.debug(" e.coords.longitude: " + e.coords.longitude);
            if(map !== null) {
                map.setLocation({
                    latitude:e.coords.latitude,
                    longitude:e.coords.longitude,
                    animate:true
                });
            }
        }

        Titanium.API.debug('updateCurrentLocation(e) - END');
    }

    function updateMapRegion(e) {
        Titanium.API.debug('updateMapRegion(e) - BEGIN');

        Ti.API.debug('e.latitude  = ' + e.latitude);
        Ti.API.debug('e.longitude = ' + e.longitude);

        Titanium.API.debug('updateMapRegion(e) - END');
    }

    function setInitialLocation(e) {

        Titanium.API.debug('setInitialLocation(e) - BEGIN');

        if (!e.success || e.error) {
            Ti.API.debug("Error: "+e.error);
            return;
        }

        Titanium.API.info('geo - current location: ' + ' long ' + e.coords.latitude + ' lat ' + e.coords.longitude);

        if(map === null) {

            map = Titanium.Map.createView({
                mapType: Titanium.Map.STANDARD_TYPE,
                region: {
                    latitude:e.coords.latitude,
                    longitude:e.coords.longitude,
                    latitudeDelta:0.007,
                    longitudeDelta:0.007
                },
                animate:true,
                regionFit:true,
                userLocation:true
            });

            mapView.add(map);

            mapView.addEventListener('regionChanged', updateMapRegion);
            Titanium.Geolocation.addEventListener('location', updateCurrentLocation);

        } else {
            updateCurrentLocation(e);
        }

        Titanium.API.debug('setInitialLocation(e) - END');

    }

    function configureAppListeners() {
        Titanium.API.debug('configureAppListeners() - BEGIN');

        if (isAndroid) {
            //  as the destroy handler will remove the listener, only set the pause handler to remove if you need battery savings
            Ti.Android.currentActivity.addEventListener('pause', function(e) {
                Ti.API.info("pause event received");

                Titanium.Geolocation.removeEventListener('location', updateCurrentLocation);
                if(mapView !== null) {
                    mapView.removeEventListener('regionChanged', updateMapRegion);
                }
            });
            Ti.Android.currentActivity.addEventListener('resume', function(e) {
                Ti.API.info("resume event received");

                Titanium.Geolocation.addEventListener('location', updateCurrentLocation);
                if(mapView !== null) {
                    mapView.addEventListener('regionChanged', updateMapRegion);
                }
            });
            Ti.Android.currentActivity.addEventListener('resumed', function(e) {
                Ti.API.info("resumed event received");

                Titanium.Geolocation.addEventListener('location', updateCurrentLocation);
                if(mapView !== null) {
                    mapView.addEventListener('regionChanged', updateMapRegion);
                }
            });
        } else {
            Titanium.App.addEventListener('pause', function(e) {
                Ti.API.info("pause event received");
            });
            Titanium.App.addEventListener('resume', function(e) {
                Ti.API.info("resume event received");
            });
            Titanium.App.addEventListener('resumed', function(e) {
                Ti.API.info("resumed event received");
            });
        }

        Titanium.API.debug('configureAppListeners() - END');

    }

    function initializeThreatMap() {
        Titanium.API.debug('ThreatMapView.createThreatMap() - BEGIN');

        if (Titanium.Platform.name == 'android') {
            isAndroid = true;
        }

        configureAppListeners();

        if(Titanium.Platform.model == 'Simulator' || Titanium.Platform.model == "sdk" || Titanium.Platform.model == "google_sdk") {
            var coords = {
                latitude: 39.4316215,
                longitude: -119.7709939
            };
            var e = {
                coords: coords,
                success: true,
                error:''
            };
            setInitialLocation(e);
        } else {
            Titanium.Geolocation.getCurrentPosition( setInitialLocation );
        }

        Titanium.API.debug('ThreatMapView.createThreatMap() - END');
    }

    MapView.createMapView = function(args) {

        mapView = Titanium.UI.createView({
            backgroundColor:'#fff'
        });

        initializeThreatMap();

        return mapView;
    };
})();
Titanium.UI.currentWindow.add(MapView.createMapView());

Attachments

FileDateSize
TC_105_Use_Case.zip2011-06-26T08:19:04.000+00002556082

Comments

  1. Paul Dowsett 2011-06-25

    In order for us to progress this issue, please edit your ticket to include a proper [use-case](http://wiki.appcelerator.org/display/guides/Contributing+to+Titanium#ContributingtoTitanium-CreatingGoodUsecases). Please state the Android version you have tested. If testing using an emulator, ensure you are *not* using 2.3.X, due to the problems explained elsewhere. Please state your Titanium build date and hash, which is output to the console before an app is launched. Also kindly read the [Submitting Bug Reports](http://wiki.appcelerator.org/display/guides/Contributing+to+Titanium#ContributingtoTitanium-SubmittingBugReports) guide before raising tickets. Thank you
  2. Joe Chavez 2011-06-26

    I have a use-case in process. The Submitting Bug Reports guide refers to a bug tracking system named "Lighthouse" is that still in use or is Jira the correct place to file bugs? http://developer.appcelerator.com/question/121625/android-uimapview-issues I have updated the Environment to include the Android version and the Titanium SDK version. Also, I've attached a "use-case code" example that reproduces the problem. The behavior described in this bug does not happen in the simulator only on an Android device.
  3. Paul Dowsett 2011-06-28

    Thank you for the updated information, Joe - the ticket is much better. :) I tried to run the project, but it failed due to a problem with MapView.js. See http://pastebin.com/7cqVvUp9 . If you can't see what the error is, I will have time to debug it on Thursday. Once we can reproduce the issue here, we can pull your code (thank you for your obvious efforts with that also, btw).
  4. Joe Chavez 2011-06-28

    Nothing in the stack trace looks familiar. I disabled Fastdev on my Mac as it was causing build issues. I've been testing my the changes I made for over a week without any issues. I have very little experience with the Android SDK and have little doubt that improvements can be made to the code. I'm planning on distributing the app in less than a week with the changes. Hopefully a fix is pushed into the master before too long.
  5. Paul Dowsett 2011-07-23

    Joe I cannot progress this without a working usecase. Are you able to supply it? Note that the guide at [Submitting Bug Reports](http://wiki.appcelerator.org/display/guides/Contributing+to+Titanium#ContributingtoTitanium-SubmittingBugReports) has now been corrected.
  6. Paul Dowsett 2011-07-23

    Joe Please do not attach a whole project - the usecase needs to be simplified and added as a single (if possible) code block in the main body of this ticket. Thanks
  7. Joe Chavez 2011-07-23

    I've inserted the code for the app.js and MapView.js files into the Description section of this ticket. The files are marked with: ========================================================== BEGIN/END File: XXXXX.js ========================================================== I hope this helps.
  8. Paul Dowsett 2011-07-25

  9. Joe Chavez 2011-07-26

    Paul, I've updated the Use Case code with the appropriate markdown tags. -Joe
  10. Paul Dowsett 2011-07-27

    Joe Thanks - the ticket looks much better. Do you have a trace log of the exception? Would you include that in a code block also? Thanks
  11. Joe Chavez 2011-07-30

    Okay... I updated to 1.7.2 and the "force close" issue is no longer present - no stack trace now. However, the missing "blue" current location map indicator flag is well... still missing on app resume.
  12. Paul Dowsett 2011-08-01

  13. Paul Dowsett 2011-10-07

    Closing due to inactivity.

JSON Source