Belgi
Belgi

Reputation: 15082

Android: Switching between two activities

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

Upvotes: 0

Views: 587

Answers (2)

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28389

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?

Upvotes: 1

Bobbake4
Bobbake4

Reputation: 24857

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.

Upvotes: 1

Related Questions