Reputation: 4860
I'm following this tutorial http://www.vogella.com/articles/AndroidIntent/article.html for having some data transfered to parent activity by calling an activity with startActivityForResult.
Basicly i’m trying to finish parent activity, on some buttons clicked but not onBackPressed. I do not know if there is a easier way or not. Like this:
MainPage to NewTarget
NewTarget to Target
// onBackPressed
Target to NewTarget
// onButtonClick on TargetActivity
Target to MainPage
The problem is onActivityResult calls when i click the button on NewTarget activity, not on Target activity finish?? Shouldn’t it be opposite?
On “NewTarget” class, i’ve got a button which needs to call an activity onClick. And i also need some feedBack on activity, named “Target”, like it’s been finished by back button or some other way (which i need to finish that activity on some point click, and i called “finish()”)
From NewTarget activity:
public void savingClick(View v) {
Intent targetIntent = new Intent(this, Target.class);
targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(targetIntent, 5);
}
On Target activity:
@Override
public void finish() {
if(ClickCheck) {
Log.d("Target","ClickCheck");
// Prepare data intent
Intent data = new Intent();
data.putExtra("returnKey", "click");
// Activity finished ok, return the data
setResult(RESULT_OK, data);
}
super.finish();
}
And finally on NewTarget activity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("NewTarget","requestCode: " + requestCode);
Log.d("NewTarget","resultCode: " + resultCode);
if(requestCode == 5 && resultCode == RESULT_OK) {
Log.d("NewTarget","data: " + data.hasExtra("returnKey"));
if(data.hasExtra("returnKey"))
this.finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
And i check the values:
// these lines have appeared when i clicked on button to startActivityForResult
requestCode: 5
resultCode: 5
// ClickCheck line has appeared on Target activity finished.
// data: data.hasExtra("returnKey")); line has never been appeared.
So what’s the deal? What am i overlooking? Thanks in advance for your helps.
Upvotes: 0
Views: 559
Reputation: 95626
To do this:
MainPage to NewTarget
NewTarget to Target
// onBackPressed
Target to NewTarget
// onButtonClick on TargetActivity
Target to MainPage
You don't need to use startActivityForResult()
unless you need to have some data returned from Target
to NewTarget
. The normal behaviour will be that pressing "back" in Target
will just call finish()
on Target and return to NewTarget
(that seems to be what you want). To get the last behaviour you should just use the following code in Target.onButtonClick()
:
Intent intent = new Intent(this, MainPage.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
This will finish both NewTarget
and Target
and then it will finish the current MainPage
activity and create a new instance of the MainPage
activity (if the launchMode of MainPage
is "standard").
Try this.
Upvotes: 1