2011-04-22 57 views
0
else{ //this is part of some onclick listener of a button in the program 
     slider.close(); 
     mapView.setClickable(false); 
     Toast.makeText(getBaseContext(), "GPS cannot retrieve your location. GPS needs clear sky or service is off. Please turn on GPS service.", Toast.LENGTH_LONG).show(); 
     final Button bt = (Button)findViewById(R.id.turnGPS); 
     bt.setVisibility(View.VISIBLE); 
     bt.setOnTouchListener(new OnTouchListener(){ 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       Intent activateGps = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       //Map.this.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
       Map.this.startActivityForResult(activateGps, RESULT_OK); 
       //bt.setVisibility(View.INVISIBLE); 
       return false; 
      } 

     }); 

    } 
} 

我想开始新的意图,所以用户可以启用GPS(如果GPS之前未激活)。我使用了startActivityForResult方法,该方法可以响应Activity是否成功。当成功时,它必须在用户界面上做一些改变....我试过并启用了GPS,但UI上仍然没有改变(例如:按钮不隐藏)。我做错了什么?onActivityResult的意图

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode==RESULT_OK) 
    { 
     if(resultCode == RESULT_OK && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
     { 
      Button bt = (Button)findViewById(R.id.turnGPS); 
      bt.setVisibility(View.INVISIBLE); 
      this.finish(); 
     } 
     else if(resultCode == RESULT_CANCELED) 
     { 
      Toast.makeText(getBaseContext(), "No GPS device or service enabled", Toast.LENGTH_LONG+Toast.LENGTH_LONG).show(); 
     } 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

这是我的onActivityResult方法。是否this.finish();当它启用时完成当前活动,因此它启动OnResume()?

回答

2

使用PreferenceActivitystartActivityForResult()(其中一种讨论参见onActivityResult() called prematurely)有一些限制。

我的建议是:启动PreferenceActivity通过startActivity()而不是startActivityForResult(),然后把你的代码需要在onStart()onResume()方法重新阅读偏好,这将立即调用的活动在顶部的堆栈。

0

有一个在Android的一个bug ...更改

android:launchMode="singleInstance(or singleTasc)" 

android:launchMode="singleTop" 
在活动

被调用startActivityForResult()方法并再次运行。它仍然会工作。

0
<activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" 
     android:alwaysRetainTaskState="True" 
     android:launchMode="singleTop"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
+0

请考虑为答案添加解释。 – Onik 2014-05-19 14:40:52

相关问题