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) {
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 :)");
}
}
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!
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:
and an BarActivity added in the AndroidManifest.xml like that:
The respective code to retrieve the intent inside the bar is in the onClicEvent of the btnBar (Button):
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:
Handling the information I did the following in the event onActivityResult() to see the retrieved information in the Bar Activity:
if you have more activities to return infomation to the parent activity is good pratices do the following:
If you can’t do this sample code..
please give me your email for me send the FooBarActivity project