Non riesco a spiegare cosa sta succedendo nel vostro codice, ma ho un progetto di esempio per fare questo ..
a FooAttività con solo un pulsante 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);
}
});
}
e un BarActivity aggiunto nel AndroidManifest.xml così:
<activity
android:name = "BarActivity">
<intent-filter>
<action
android:name = "xper.activity.ACTIVITY_BAR_INTENT"/>
<category
android:name = "android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Il rispettivo codice per recuperare l'intento all'interno della barra è nella onClicEvent del 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();
}
});
}
Ora, se non si gestisce bene l'evento onActivityResult(), quando si preme il pulsante Android "BACK", è possibile ottenere errori.
Se l'intenzione (intenzione) nell'attività B è di fornire alcune informazioni all'attività A, se si preme il pulsante indietro, non so se l'attività B sarà nello stack, ma l'intenzione non è fatto. Così ho fatto la seguente:
@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 le informazioni che ho fatto la seguente nell'evento onActivityResult() per visualizzare le informazioni recuperate nella Barra attività:
@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);
}
}
se si dispone di più attività da restituire infomation all'attività genitore è buone pratiche effettuare le seguenti operazioni:
//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;
}
}
Se non è possibile fare questo codice di esempio .. favore dammi il tuo e-mail per me invia il progetto FooBarActivity
Duplicato di http://stackoverflow.com/questions/2679250/setresult-does-not-work-when-back-button-pressed – pjv