2

I'm currently developing an app which uses GPS location on googleMaps view. I check if GPS feature is activated, and if not I redirect the user to the setting menu.

  1. If I click on Back button (by activating or not the GPS) I come back to my app - works fine
  2. If I click on Home button, and then relaunch my app, I am directly redirected to the GPS setting menu => my intent is still on.
    The problem is that I do not know when kill this intent.

Here is the incriminated part of my code:

    Button btnLocateMe = (Button)findViewById(Rsendlocationinfo.id.locateme);
    btnLocateMe.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Context context = getApplicationContext();

            objgps = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

            //Check GPS configuration
            if ( !objgps.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {  

                //Warn the user that its GPS is not activated
                Toast gpsActivationCheck = Toast.makeText(context, "GPS deactivated - Touch to open the GPS settings.", Toast.LENGTH_LONG);
                gpsActivationCheck.show();

                //Open the GPS settings menu on the next onTouch event
                //by implementing directly the listener method - dirt manner
                mapView.setOnTouchListener(new OnTouchListener() {
                    public boolean onTouch(View v, MotionEvent event) {  

                        //And here is my problem - How to kill this process
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);  

                        return false;
                    }
                });      
            }
            else {  
                //Location acquisition.  
            }  

I tried some stopself(), finish(), in onStop(), onDestroy(), onPause()... It crashes or does not do anything... Could you please give me a hand ?

Fuzzical Logic
  • 12,947
  • 2
  • 30
  • 58
Mario
  • 405
  • 7
  • 15

3 Answers3

6

Ok, I have finally found.
I will try to explain the difference in the behavior clearly:

  1. Old behavior:

    • Launch application
    • Google maps activity starts
    • User accepts redirection to the setting menu to activate GPS
    • Setting menu displayed
    • User click on "Home" button
    • User relaunch the application => the setting menu is displayed, as it is the latest intent in the history stack of the application.

The code to launch this intent was:

startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
  1. New behavior:

    • Launch application
    • Google maps activity starts
    • User accepts redirection to the setting menu to activate GPS
    • Setting menu displayed
    • User click on "Home" button
    • User relaunch the application => The Google maps is displayed, and not the setting menu, because I have added the flag FLAG_ACTIVITY_NO_HISTORY to the redirection intent.

My code is now:

Intent intentRedirectionGPSSettings = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intentRedirectionGPSSettings.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

startActivityForResult(intentRedirectionGPSSettings, 0);

I tried to explain well. The tag "android:noHistory(true)" in the manifest.xml works for activities, but the flag FLAG_ACTIVITY_NO_HISTORY is a good alternative, as it works for intents.

Thank you for help.

Mario
  • 405
  • 7
  • 15
1

The Location Settings Activity has now become part of your application/tasks when you invoked it from your calling logic. As far as Android is concerned this was the last screen from "your" application before your app became inactive. So, when you go back into the application again it will go back to that Activity automatically.

This is the normal and documented behavior. If you don't want that Activty to be there when you go back then try calling "finishActivity()" ... this only works for those Activities that were started via the startActivityForResult() method.

BonanzaDriver
  • 6,411
  • 5
  • 32
  • 35
  • I do not know how to declare the onStop() method for this activity. Indeed this startActivityForResult is sent by a "mother" activity, and the onStop() method I define in this .java file concerns only this mother activity. Then, when I do "finishActivity(0);" in the onStop() method, it closes directly the setting menu has the onStop() is called during the redirection. I would like to close the redirectin activity when I leave the setting menu, not before. Idea ? – Mario Jul 04 '11 at 23:29
  • Once on the Setting menu, clicking on Home button do not even call onDestroy(), so I cannot add the finishActivity() in there... I have seen that even the google navigation apps do not manage this. Once you have activated the GPS in the setting menu, if you click the back button, you are not redirected to the application, but on the home page. – Mario Jul 04 '11 at 23:46
  • Read up on the documenation if you can ... override the onStop() or any of the other "on....XXX()" methods (onRusume(), onPause(), onDestroy(), etc., etc., etc.). Your mother Activity can call the childs's finishActivity() if you want that Location settings screen to be removed so it won't appear again when the application restarts later. – BonanzaDriver Jul 05 '11 at 03:24
  • Actually that's what I do. I call the finishActivity() of the child activity (started with startActivityForResult()). But there is no method called when you go back to your application when I use the emulator (onResume() and onStart() are not called). On my phone it works... – Mario Jul 05 '11 at 18:16
0

use android:launchMode="singleTask" in AndroidManifest

Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
Andrew Nodermann
  • 610
  • 8
  • 13