This is my problem, I have the main view which only shows one button, pressing this button another view is shown. This view has only another button, when this button is push
here,
@Override
public void onBackPressed() {
setResult(Activity.RESULT_OK);
finish();
}
does work to return(RESULT_OK) by pressing the BACK button. Do NOT call
super.onBackPressed()
.
Use this
Intent returnIntent = new Intent();
setResult(RESULT_OK,returnIntent);
instead of
setResult(RESULT_OK);
only
I can't explain what is happening in your code but I have a project sample to do this..
a FooActivity with just a button btnFoo:
@Override
protected void onStart() {
super.onStart();
btnFoo.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivityForResult(new Intent("xper.activity.ACTIVITY_BAR_INTENT"),1);
}
});
}
and an BarActivity added in the AndroidManifest.xml like that:
<activity
android:name = "BarActivity">
<intent-filter>
<action
android:name = "xper.activity.ACTIVITY_BAR_INTENT"/>
<category
android:name = "android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
The respective code to retrieve the intent inside the bar is in the onClicEvent of the btnBar (Button):
@Override
protected void onStart() {
super.onStart();
btnBar.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
intent.putExtra("codBar", "bar");
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}
Now, if you doesn't handled well the onActivityResult() event, when you press the Android button "BACK", you can get errors.
If the Intent (intention) in the Activity B is to give some information to the activity A, if you press the button back, I don't know if the activity B will be in the stack, but the intention isn't done. So I did the following:
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
//Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
//intent.putExtra("codBar", "bar");
//setResult(Activity.RESULT_CANCELED, intent);
setResult(Activity.RESULT_CANCELED);
finish();
}
Handling the information I did the following in the event onActivityResult() to see the retrieved information in the Bar Activity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data != null) {
Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode == " + resultCode + "\tdata == " + data, 10000).show();
btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode == " + resultCode + "\tdata == " + data /*+ "extras == " + data.getExtras().getString("codBar")*/);
} else {
Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode == " + resultCode, 10000).show();
btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode == " + resultCode);
}
}
if you have more activities to return infomation to the parent activity is good pratices do the following:
//put private static final int globals atributes with the respective name of the
//activity to represent the requestCode for each activity you have like:
private static final int ACTIVITY1 = 117;
private static final int ACTIVITY2 = 118;
...
private static final int ACTIVITYN = 215;
//In the event onActivityResult() is better use the switch statement to handle each
//specific activity to catch information
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_CANCELED) return; // breaks
//if you decide to handle some information of Activity.RESULT_CANCELED
//ignore the above condition that returns and handle it inside the switch statement
switch(requestCode) {
case ACTIVITY1:
{
//Dosomething
} break;
case ACTIVITY2:
{
//Dosomething
} break;
...
case ACTIVITYN:
{
//Dosomething
} break;
}
}
If you can't do this sample code.. please give me your email for me send the FooBarActivity project