0

I have this code :

 Button groupsButton = (Button)findViewById(R.id.groupsButton); groupsButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent myintentGroups=new Intent(CreateMessageActivity.this, GroupsActivity.class).putExtra("<StringName>", "Value"); startActivityForResult(myintentGroups, 3); } }); 

and now I want to write the onActivityResult, I tried adding this code inside the onClick but it's not working (Eclipse gives me an error):

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); String result_string=data.getStringExtra("<StringName>"); } 

Is the code for writing onActivityResult wrong or maybe I'm putting it in the wrong place ?

** Edit : ** the code :

 Button groupsButton = (Button)findViewById(R.id.groupsButton); groupsButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent myintentGroups=new Intent(CreateMessageActivity.this, GroupsActivity.class).putExtra("<Came From Create Message>", "Value"); startActivityForResult(myintentGroups, 3); } }); @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); String result_string=data.getStringExtra("<StringName>"); } 

Edit 2: The errors: Multiple markers at this line - Syntax error on token "(", ; expected - void is an invalid type for the variable onActivityResult - Syntax error on token ")", ; expected - Syntax error on token ",", ; expected - Syntax error on token ",", ; expected

0

2 Answers 2

1

onActivityResult should be placed in the Activity class that contains the onClick not in the actual onClick. The CreateMessageActivity.this in the new Intent will indicate which activity the result should be returned to.

Sign up to request clarification or add additional context in comments.

7 Comments

Eclipse gives me an error : void is an invalid type for the variable onActivityResult
oh, then you're probably not putting it in the right place... it should be in the body of your class, at the same level (sibling of) as onCreate
@Dr.Dredel - it's in the Activity class that contains the onClick
well... the code you have, as posted, is correct... so, you'll need to post more code, cause as is you should not be seeing any errors
Just to be clear the onActivityResult should be a standalone function in the Activity class. It should not be included inside of any function already in the class.
|
1

Are you sure that you are returning to this activity? What do you do in your GroupsActivity.class? How do you exit from it? The way to get back to THIS activity would be to call finish() in the GroupsActivity.class... then you should get your string. If you are calling another startActivity() in your GroupsActivity.class then you're actually not coming "back" to THIS one, you're going forward to another instance of it.

also, in your displayed code you're not doing anything with the string... are you sure it's not already working correctly?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.