简体   繁体   中英

back button not taking me to previous activity, rather home screen

In my activity, I am performing a check if both the GPS and WIFI location servies are off.

Incase both are off, the intent of the location services is triggered. However, when I press the back button, I cannot come back to my activity, rather homescreen. Thus the screen is stuck in the location settings page.

Note that I am extending FragmentActivity and using SupportMapFragment.

        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    if (status != ConnectionResult.SUCCESS)
    { // Google Play Services are not available

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,requestCode);
        dialog.show();

    } else
    { 

        fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = fm.getMap();
        googleMap.setMyLocationEnabled(true);

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if(locationManager != null)
        {
            gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if(gpsIsEnabled==false && networkIsEnabled==false)
            {
                Log.i("code","both wifi and gps off.");
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, 1);
            }
            else
            {
                Criteria criteria = new Criteria();
                String provider = locationManager.getBestProvider(criteria, true);

                location = locationManager.getLastKnownLocation(provider);

                if (location != null)
                {
                    onLocationChanged(location);
                }

                locationManager.requestLocationUpdates(provider, 20000, 0, this);
            }
        }
        else
        {
            Log.i("app","null.");
        }

Make sure you have ACCESS_FINE_LOCATION in your Manifest.

public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
            // If Gps and Wifi are off

               Intent mIntent= new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
               mIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
               startActivityForResult(mIntent, 1);
        }
    });
}

@Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == 1) {
       switch (requestCode) {
          case 1:
               // Fetch Location..
           break;
        }
     }  
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM