2011-04-22 97 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

相關問題