Android getting RESULT_CANCELED when I specifically add RESULT_OK
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 this current view finishs and the control backs to the previous view.
To show the second view I use startActivityForResult, I put the code here.
private void startNewview() {
Intent it = new Intent(getApplicationContext(), newView.class);
startActivityForResult(it,VIEW_ID);
}
The view called only has a button event, here is the code
Button b = (Button) findViewById(R.id.close);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setResult(RESULT_OK);
finish();
}
});
And finally, the method onActivityResult in the main view, here is the code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
s开发者_如何学JAVAuper.onActivityResult(requestCode, resultCode, data);
if(requestCode == VIEW_ID && resultCode == RESULT_OK) {
tv = (TextView) findViewById(R.id.tv);
tv.setText("The result ok is here :)");
}
}
The problem is resultCode always is 0 = RESULT_CANCELED and I do not know how to solve it, can anyone help me?
Thank you very much!
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()
.
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
Use this
Intent returnIntent = new Intent();
setResult(RESULT_OK,returnIntent);
instead of
setResult(RESULT_OK);
only
精彩评论