Passing data to main activity is lost

android, android-activity, android-intent

Solution

The problem is with the class returning the result.

As Mikel points out in another answer:

Intent(Context packageContext, Class cls) Added in API level 1

Create an intent for a specific component. All other fields (action, data, type, class) are null, though they can be modified later with explicit calls. This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you; see setComponent(ComponentName) for more information on the repercussions of this.

You aren't explicitly setting the data parameter as the documentation above states but instead attempting to add an Extra to a null Data object which fails.

Instead, I would change your class to the following:

public class SearchActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        btnSend.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String videoUrl = "http://video-url.com";
                Intent data = new Intent();
                data.putExtra("VIDEO_URL", videoUrl);
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }
}

This will initialize your Intent and allow you to add Extra data to it (this is also the normal way you would send back data, there isn't a need normally to send back a result which is tied to a specific Intent.

Problem

That issue took me few days of head-banging with no avail. In short I have two `Activities` for sake of the example I'll call them `MainActivity` and `SearchActivity`. The `MainActivity` has a button that takes you to a search screen which is the `SearchActivity` and there I have a button with preset value that I want to pass back to the `MainActivity`. So far so good. I click the button in debugger I can see my extra values and next breakpoint is in the onCreate in `MainActivity` when I get the extras there is nothing, none, all gone. SO here is the important part, the code: MainActivity.class ``` public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent search = new Intent(v.this, SearchActivity.class); startActivityForResult(search, SEARCH_VIDEO_REQUEST); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SEARCH_VIDEO_REQUEST) { if (resultCode == RESULT_OK) { String url = data.getExtras().getString("VIDEO_URL"); if (url != null) { txtUrl.setText(url); } } } } } ``` SearchActivity.class ``` public class SearchActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { btnSend.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String videoUrl = "http://video-url.com"; Intent data = new Intent(SearchActivity.this, MainActivity.class); data.putExtra("VIDEO_URL", videoUrl); setResult(RESULT_OK, data); finish(); } }); } } ``` AndroidManifest.xml ``` <activity android:name="com.project.MainActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize" android:theme="@android:style/Theme.Black.NoTitleBar" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.MEDIA_MOUNTED"/> <data android:scheme="file"/> </intent-filter> </activity> <activity android:name="com.project.SearchActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait"> </activity> ```

Original source