我有一个活动“ SearchActivity”,它扩展了SherlockActivity。在此活动中,我有一个选项菜单,其中包含一个搜索栏和一个“转到”按钮。在搜索栏中输入的数据必须传递到扩展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();
    }
}


问题是onActivityResult()方法从未在NavigationActivity中被调用。按下“执行”按钮后,我可以从SearchActivity导航到NavigationActivity,但无法在NavigationActivity中获取数据。您能在这方面帮助我吗?

谢谢。

最佳答案

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

导航活动

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();

        }
    });
}
}

10-07 15:36