问题
Though there are several questions regarding this topic, I could not find a right answer to this.
I have a main activity (This is one activity in a tabview) from where I am calling the login activity.
Button chdbtn=(Button)findViewById(R.id.Add);
chdbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(main.this, Login.class);
startActivityForResult(myIntent, 1001);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 1001)
{
if(resultCode == RESULT_OK)
{
Log.i("Info","Inside");
}
}
}
And in my login class, When I click on a button, I am doing this
Button chdbtn=(Button)findViewById(R.id.Addchild);
chdbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent Ireturn = new Intent();
setResult(RESULT_OK,Ireturn);
finish();
}
});
But when I click on the button in login activity, the control is not coming to the main activities onActivityResult method. Can anyone guide me what is the issue.
VIJAYapp.sample.ChildEntry1$1/onClick:23
INFO/ActivityManager(59): Starting activity: Intent { cmp=app.sample/.ChildLogin }
WARN/ActivityManager(59): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=app.sample/.ChildLogin }
DEBUG/PhoneWindow(999): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@43e4b620 has no id.
INFO/ActivityManager(59): Displayed activity app.sample/.ChildLogin: 460 ms (total 460 ms)
INFO/VIJAY(999): VIJAYapp.sample.ChildLogin$1/onClick:24
Above is the log that I am getting when clicking on the buttons. I can see that there is some problem with the Activity..any answers?
回答1:
I think your problem is solved now. I made some modifications to your code and the onActivityResult is now called (check the modified code: http://www.4shared.com/file/_VR3zi28/CopySampleApptar.html?):
1.-When you call the Login activity class use: getParent().startActivityForResult(myIntent, 1001);
I am not very skilled in Android but I understand that the one controlling the flow among activities is the ActivityGroup class, so you should start the activities using the ActivityGroup instance. In the previous line getParent() makes reference to the ActivityGroup.
2.-Because you use the ActivityGroup instance, your onActivityResult must be placed in that class as well.
回答2:
Consider replacing the lines
Intent Ireturn = new Intent();
setResult(RESULT_OK,Ireturn);
with
setResult(RESULT_OK,getIntent());
getIntent() returns the Activity that started Login.
来源:https://stackoverflow.com/questions/6344051/onactivityresult-is-not-been-invoked-after-the-child-activity-calls-finish