简体   繁体   English

Android:在两个活动之间切换

[英]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): 现在我想编写onActivityResult,我尝试在onClick中添加此代码,但它不起作用(Eclipse给我一个错误):

@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 ? 写onActivityResult的代码是错误的还是我把它放在错误的地方?

** 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 ")", ; 编辑2:错误:此行的多个标记 - 令牌上的语法错误“(”,; expected - void是变量onActivityResult的无效类型 - 令牌上的语法错误“),; expected - Syntax error on token ",", ; expected - 令牌上的语法错误“,”,; expected - Syntax error on token ",", ; expected - 令牌上的语法错误“,”,; expected 预期

onActivityResult should be placed in the Activity class that contains the onClick not in the actual onClick. onActivityResult应该放在包含onClick而不在实际onClick中的Activity类中。 The CreateMessageActivity.this in the new Intent will indicate which activity the result should be returned to. 新Intent中的CreateMessageActivity.this将指示应返回结果的活动。

Are you sure that you are returning to this activity? 你确定要回到这个活动吗? What do you do in your GroupsActivity.class? 您在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. 回到这个活动的方法是在GroupsActivity.class中调用finish()然后你应该得到你的字符串。 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. 如果你在GroupsActivity.class中调用另一个startActivity(),那么你实际上并没有“回到”这个,你将前进到另一个实例。

also, in your displayed code you're not doing anything with the string... are you sure it's not already working correctly? 另外,在你显示的代码中,你没有对字符串做任何事情......你确定它还没有正常工作吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM