0

In my main activity i have this code:

noConnectionButton.setOnClickListener(new OnClickListener() {

@Override
        public void onClick(View v) {
            startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS), SET_CONNECTION);

        }
    });

in the onCreate method. Then:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == SET_CONNECTION){

        checkConnection();

    }

}

So what I want to do is to start the settings activity in a way that the user can switch on wireless or data connection. My problem is that the onActivityResult is called prematurely!!! so I want it to be called only after the user backs to my activity. any suggestion??

here the manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.polimi.metalnews"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="13"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="it.polimi.metalnews.MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="it.polimi.metalnews.HomeActivity"
        android:label="@string/title_activity_home" >
    </activity>
    <activity
        android:name="it.polimi.metalnews.NewsActivity"
        android:label="@string/title_activity_news" >
    </activity>
    <activity
        android:name="it.polimi.metalnews.AlbumActivity"
        android:label="@string/title_activity_album" >
    </activity>
    <activity
        android:name="it.polimi.metalnews.ContestActivity"
        android:label="@string/title_activity_contest" >
    </activity>
</application>

Tunarock
  • 127
  • 1
  • 1
  • 12

2 Answers2

0

follow this process:

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// Check which request we're responding to

if (requestCode == PICK_CONTACT_REQUEST) {
    // Make sure the request was successful
    if (resultCode == RESULT_OK) {
        // The user picked a contact.
        // The Intent's data Uri identifies which contact was selected.

        // Do something with the contact here (bigger example below)
    }
}

}

use below link for further detail:

http://developer.android.com/training/basics/intents/result.html

Yogendra
  • 4,817
  • 1
  • 28
  • 21
0

In onActivity result first check if the result is came from the action is OK or not by comparing your result code to RESULT_OK like:

if (resultCode == RESULT_OK) { // You successfully got back then do what ever you want. //Then you can check you particular data according to your request. }

Because there is no reason for going deep if the result is not ok. Every system service will result in RESULT_OK or RESULT_CANCEL if canceled by user.

I don't think there is anything wrong with intent declaration but i read this kind of article link it explains your problem in a better manner

It is due to the Activity is SingleTask and whenever it is called it reports the result.

Community
  • 1
  • 1
varun bhardwaj
  • 1,522
  • 13
  • 24
  • Ok thanks, I've missed that, but still the onActivityResult is called immediately. Maybe I'm wrong with the intent declaration? or a particular Activity setting? – Tunarock Feb 24 '14 at 09:45
  • @Tunarock See your launch mode of this activity – varun bhardwaj Feb 24 '14 at 10:00
  • I have to set launch mode to a particular value? – Tunarock Feb 24 '14 at 10:03
  • What is current value of "android:launchMode" in Manifest for this activity? – varun bhardwaj Feb 24 '14 at 10:03
  • the default I presume, because I haven't set it – Tunarock Feb 24 '14 at 10:14
  • I have tested this code on my S3 and it works fine. When i call Settings the screen opens up and when i press back onActivityresult was called. So can you mention the version and device you are testing on and does the settings screen appears in your device or it stays on your applicaiton only. – varun bhardwaj Feb 24 '14 at 11:06