Android: Subactivity returning NULL Intent object in onActivityResult
Can't figure this one out. Done research, searched here, online, etc. Still a new programmer to Java/Android, but it seems like I'm doing this right, so here goes.
My main activity starts several sub-activities based on touched menu entries. For each of these I pass a bundle to the subactivity with some "extra" string data. Each subactivity retrieves the data, does some processing, then puts new/changed data back into a bundle with .putExtra() and exits the subactivity with a finish().
This is working fine in all but ONE of the subactivities. in this one subactivity, when I return back to onActivityResult, the intent object is NULL instead of "has extras". I've tried numerous things, but I just can't get it working. Here is some skeleton code which I hope will help prove the point:
In the parent activity, part of onOptionItemsSelected, the subactivities are started:
case R.id.read_file:
Intent intentR = new Intent(this, GetReadFileName.class);
intentR.putExtra(GetReadFileName.EXTRA_WHATEVER, baseDirectory);
startActivityForResult(intentR, REQUEST_READ_FILE_NAME);
return true;
case R.id.write_file:
Intent intentW = new Intent(this, GetWriteFileName.class);
intentW.putExtra(GetWriteFileName.EXTRA_FILE_NAME, fileName);
startActivityForResult(intentW, REQUEST_WRITE_FILE_NAME);
return true;
Sub-activity GetWriteFileName:
public class GetWriteFileName extends Activity{
String fileName;
public static String EXTRA_FILE_NAME = "file_name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wfn);
fileName = getIntent().getExtras().getString(EXTRA_FILE_NAME);
// fileName is PROPERLY received from parent. No issues here.
.
. do processing
.
Intent intent = new Intent();
intent.putExtra(EXTRA_FILE_NAME, fileName); // put changed/modified name in bundle for parent
setResult(Activity.RESULT_OK, intent);
finish();
Sub-Activity GetReadFileName:
public class GetReadFileName extends Activity {
/** Called when the activity is first created. */
public static String EXTRA_WHATEVER = "whatever";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String extStorageDir = Environment.getExternalStorageDirectory().toString() + getIntent().getExtras().getString(EXTRA_WHATEVER);
// extStorageDir is PROPERLY retrieved from parent. No issue here.
.
. do processing
.
Intent intent = new Intent();
intent.putExtra(EXTRA_WHATEVER, "Just a damn string"); // Stuff to pass back OUT to parent
// Note: doesn't matter if I have a literal, String variable, or anything else here. End result is the same.
setResult(Activity.RESULT_OK);
finish();
And, finally, in onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_WRITE_FILE_NAME:
if (resultCode == Activity.RESULT_OK) {
fileName = data.getExtras().getString(GetWriteFileName.EXTRA_FILE_NAME);
}
break;
case REQUEST_READ_FILE_NAME:
if (resultCode == Activity.RESULT_OK) {
fileName = data.getExtras().getString(GetReadFileName.EXTRA_WHATEVER);
}
开发者_C百科 break;
You will, I hope, forgive the leaving out of closing braces, etc.
The problem is, when GetWriteFileName returns to the parent Intent data shows in the debugger as "has extras" and the string can be retrieved.
When GetReadFileName returns to the parent Intent data is NULL, and that's the problem. Why is it NULL? From my point of view I'm doing the same thing in both activities to return stuff back to the parent. My Android Manifest file contains what I think it should. The key fields seem correct. I'm stuck here. Ideas?
Section of Manifest file:
<activity android:name=".DeviceListActivity"
android:label="@string/select_device"
android:theme="@android:style/Theme.Dialog"
android:configChanges="orientation|keyboardHidden" />
<activity android:name=".GetWriteFileName"
android:theme="@android:style/Theme.Dialog"
android:configChanges="orientation|keyboardHidden" />
<activity android:name=".GetReadFileName"
android:theme="@android:style/Theme.Dialog"
android:label="@string/select_file"
android:configChanges="orientation|keyboardHidden" />
/>
Thanks for reading through all this.
In your write subactivity, you pass back the intent. In your read one, you don't.
setResult(Activity.RESULT_OK, intent);
instead of
setResult(Activity.RESULT_OK);
精彩评论