onActivityResult() is not called after I run finish in the child activity
I'm attempting to pass data between two activities with startActivityForResult(). Here is my code:
//Within an onOptionsItemSelected method
//..
intent.setClass(this, FileManager.class);
startActivityForResult(intent, 0);
//..
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
Log.w("result Recieved", "request code: " + requestCode + ", result code: " + resultCode);
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 0 && resultCode == Activity.RESULT_OK){
extras = intent.getExtras();
if(extras != null){
Log.w("result Recieved", "extras non null\n" + extras.toString());
root = new File(extras.getString("SelectedDir"));
fileName = extras.getString("SelectedFile");
loadFile(root, fileName);
}
}
}
In the FileManager I have:
//..
Intent i = new Intent();
Bundle bundle = new Bundle();
bundle.putString("selectedFile", file.getName());
bundle.putString("selectedDir", file.getPath());
i.putExtras(bundle);
setResult(RESULT_OK, i);
Log.d("Finishing status:", "Finishing");
finish();
//..
It doesn't receive a result until I re-open the activity at which point FileManager is forced t开发者_如何学Co close. This causes the request and result code to be 0, indicating failure. The FileManager class works by displaying a list of files, when one is selected an alert dialog appears for confirmation and in the listener for yes input I call the method detailed above. How do I make it return a result following the call to finish?
EDIT:
I believe the problem is a listener or something continuing to run after finish is called. I'm not sure what though. I've written a test application that switches between two activities with buttons and it works, I have basically the same code as this project without all the extra methods.
EDIT2: I commented out basically everything in the FileManager class and it still doesn't return the result right. So, it must be a problem with the activity that starts it.
EDIT3: There error was in some old code I forgot to delete, before the switch case statement I put the FLAG_ACTIVITY_NEW_TASK flag on the intent.
I had unnecessary flags on the intent. Also I need to remember to remove old code I used for testing.
精彩评论