1

我有一个扩展 SherlockActivity 的活动“SearchActivity”。在这个活动中,我有一个选项菜单,其中有一个搜索栏和一个“开始”按钮。在搜索栏中输入的数据必须传递给扩展 SherlockMapActivity 的前一个活动“NavigationActivity”。

这是我的 SearchActivity 代码的一部分:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.go:
           Intent intent = new Intent();
           intent.putExtra("SearchText", enteredText);
           setResult(200, intent);
           startActivityForResult(intent, 100);
           finish();
           break;

    }
    return true;
}

这是 NavigationActivity 中的 onActivityResult() 方法:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    System.out.println("In onActivityResult method");
    if(data != null && resultCode == 200 && requestCode == 100) {
        String text = data.getStringExtra("SearchText");
        System.out.println("The data is: " + text);
        Toast.makeText(this, "The text entered in the search bar is: " + text, Toast.LENGTH_LONG).show();
    }       
}

问题是在 NavigationActivity 中永远不会调用 onActivityResult() 方法。按下“开始”按钮后,我可以从 SearchActivity 导航到 NavigationActivity,但无法获取 NavigationActivity 中的数据。你能在这方面帮助我吗?

谢谢你。

4

3 回答 3

1

请参阅下面的示例代码并相应地修改您的代码。

导航活动

public class NavigationActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigationActivity);

    Button btnTest = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
                    {
          Intent intent = new Intent(Launcher.this,Activity2.class);
          startActivityForResult(intent, 100);
        }

    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode ==  200)
    {

        String enteredText = "no action defined for this requestcode :"+resultCode;
        if(requestCode == 100)
        {
            enteredText = (String)data.getStringExtra("SearchText");

        }
        Toast.makeText(Launcher.this,enteredText,Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(Launcher.this,"some exception occurred",Toast.LENGTH_SHORT).show();
    }
}
}

搜索活动

public class SearchActivity extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    Button btnGO = (Button)findViewById(R.id.buttonGo);
    btnGO .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            EditText edtSearchBar = (EditText )findViewById(R.id.tvTest);

               intent.putExtra("SearchText", edtSearchBar .getText().toString());

            setResult(200,intent);
            finish();

        }
    });
}
}
于 2012-09-18T07:02:25.183 回答
1

不需要反过来吗?

据我了解,您需要startActivityForResult(..)从您的NavigationActivity而不是从SearchActivity.

NavigationActivity

Intent intent= new Intent(context, SearchActivity.class);
startActivityForResult(intent, 200);

然后在SearchActivity通话中

setResult(..);
finish();

将结果返回给调用活动并触发您的onActivityResult(..)方法。

请参阅此相关答案

于 2012-09-18T07:10:19.717 回答
0

startActivityForResult用于将被调用Activity的数据返回给调用Activity,即如果ActivityA正在调用B,则可以使用startActivityForResultB返回数据给A

于 2012-09-18T07:10:43.510 回答