Edit: the answer is not to call startActivityForResult on the called Activity object, instead invoke it from the caller’s context.
This is my code, exception on last line:
107 Intent intent = new Intent(CallingActivity.this, CalledActivity.class);
108 CalledActivity calledActivity = new CalledActivity();
109 calledActivity.startActivityForResult(intent, CALLED_ACTIVITY_CODE);
Inside CallingActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(SR.tag,"req=" + requestCode + " res=" + resultCode); //Never getting here
}
Manifest file snippet:
<activity android:name=".ui.CallingActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ui.CalledActivity"></activity>
Exception:
01-14 11:30:14.850: ERROR/AndroidRuntime(1036): FATAL EXCEPTION: main
01-14 11:30:14.850: ERROR/AndroidRuntime(1036): java.lang.NullPointerException
01-14 11:30:14.850: ERROR/AndroidRuntime(1036): at android.app.Activity.startActivityForResult(Activity.java:2833)
01-14 11:30:14.850: ERROR/AndroidRuntime(1036): at com.litvin.app.ui.CallingActivity.onSharedPreferenceChanged(CallingActivity.java:109 )
...
Whenever I move the HOME and LAUNCHER intent filters into .ui.CalledActivity, it works fine. It starts CalledActivity when I press the icon.
I have looked through many similar answers here, but none worked for me.
// no need to create any object for that
//do this