2011-02-04 9 views
13

Questo è il mio problema, ho la vista principale che mostra solo un pulsante, premendo questo pulsante viene mostrata un'altra vista. Questa vista ha solo un altro pulsante, quando questo pulsante viene premuto per completare la vista corrente e il controllo torna alla vista precedente.Android ottenendo RESULT_CANCELED quando aggiungo specificamente RESULT_OK

Per mostrare la seconda vista, utilizzo startActivityForResult, ho inserito il codice qui.

private void startNewview() {  
    Intent it = new Intent(getApplicationContext(), newView.class); 
    startActivityForResult(it,VIEW_ID); 

} 

La vista chiamato solo ha un evento tasto, ecco il codice

Button b = (Button) findViewById(R.id.close); 
    b.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      setResult(RESULT_OK);    
      finish(); 

     } 
    }); 

E, infine, il metodo onActivityResult nella vista principale, ecco il codice

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode == VIEW_ID && resultCode == RESULT_OK) { 
     tv = (TextView) findViewById(R.id.tv); 
     tv.setText("The result ok is here :)"); 
    } 

} 

Il il problema è resultCode è sempre 0 = RESULT_CANCELED e non so come risolverlo, qualcuno può aiutarmi?

Grazie mille!

+0

Duplicato di http://stackoverflow.com/questions/2679250/setresult-does-not-work-when-back-button-pressed – pjv

risposta

5

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

+0

in realtà il tuo "onBackPressed()" non ha funzionato qui a meno che non rimuovo il (superfluo) super.OnBackPressed(). – chksr

+0

'' 'new Intent (" xper.activity.ACTIVITY_BAR_INTENT "), 1)' '' non corrisponde ad alcun costruttore –

-1

Utilizzare questa

Intent returnIntent = new Intent(); 
    setResult(RESULT_OK,returnIntent); 

invece di

setResult(RESULT_OK); 

solo

+0

Ho provato questa soluzione ma non funziona, ottengo lo stesso: S –

+0

Stai usando finish() dopo setResult –

6

qui,

@Override 
public void onBackPressed() { 
    setResult(Activity.RESULT_OK); 
    finish(); 
} 

funziona per tornare (RESULT_OK) premendo il tasto BACK. NON chiamare

super.onBackPressed().

+0

Questa è la risposta effettivamente corretta. –

Problemi correlati