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

        });

    }
}

I want to start new intent so user can enable GPS (if GPS is not activated before). I used startActivityForResult method that can respond whether Activity has resulted success or not. When success it has to do some changes on the user interface....I tried and I enable the GPS but still no changes on the UI (e.g: button does not hide). What I'm doing wrong?

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

Here is my onActivityResult method. Does this.finish(); finishes the current activity when it is enabled, so it initiates OnResume()?

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148

3 Answers3

2

There are some restrictions while using PreferenceActivity with startActivityForResult() (one of these kind of discussion see onActivityResult() called prematurely).

My suggestion is: start the PreferenceActivity by startActivity() instead of startActivityForResult(), and then put your code that needs to re-read the preferences in onStart() or onResume() method, which will be invoked immediately as the Activity is on the top of the stack.

Community
  • 1
  • 1
shihpeng
  • 5,283
  • 6
  • 37
  • 63
0

There's a bug in Android... Change your

android:launchMode="singleInstance(or singleTasc)"

to

android:launchMode="singleTop"

in your Activity which is calling the startActivityForResult() method and run again. It still will be working.

War10ck
  • 12,387
  • 7
  • 41
  • 54
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>