【问题标题】:back button not taking me to previous activity, rather home screen后退按钮不会带我进入上一个活动,而是主屏幕
【发布时间】:2013-02-24 11:56:05
【问题描述】:

在我的活动中,我正在检查 GPS 和 WIFI 定位服务是否都关闭。

如果两者都关闭,则会触发定位服务的意图。 但是,当我按下返回按钮时,我无法返回我的活动,而是返回主屏幕。 因此屏幕卡在位置设置页面中。

请注意,我正在扩展 FragmentActivity 并使用 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.");
        }

【问题讨论】:

  • “屏幕卡在位置设置页面”是什么意思?您的问题是“当您从位置服务按回时,您会到达主屏幕而不是您的活动?”
  • 没有。我的主要活动扩展了片段活动加载,并且在 onCreate() 内部有一个意图打开位置设置页面(我可以在其中打开或关闭 gps),但是,一旦我完成了我必须在那里做的事情,按后退按钮不会让我回到我的主要活动。它只是将我带到手机的主屏幕(就像按下主页按钮一样)。即,后退按钮不会将我带到堆栈中的上一个活动。
  • Mitesh,我添加了代码。
  • 请张贴完整的代码,特别是您为后压所做的代码
  • Mitesh,这个 fragmentActivity 中没有后按的代码,我正在从 ACTION_LOCATION_SOURCE_SETTINGS 进行后按,但它仍然存在。我不知道 fragmentActivities 是否不像正常活动那样处理后按。

标签: android gps maps location google-maps-android-api-2


【解决方案1】:

确保您的清单中有ACCESS_FINE_LOCATION

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;
        }
     }  
 }

【讨论】:

  • 你提到权限了吗??
  • @tony9099 看到这个链接stackoverflow.com/questions/5987379/…
  • Mitesh,还是不行。我还意识到后按有时会起作用,但是,在使用几次后,它会导致应用程序冻结并变慢。
  • 你是否设置了任何 onPause() 或 onResume()?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
相关资源
最近更新 更多