onActivityResult()方法未在扩展SherlockMapActivity的活动中调用

时间:2012-09-18 06:56:57

标签: android android-intent actionbarsherlock actionbarsherlock-map

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

这是我的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中的数据。你能在这方面帮助我吗?

谢谢。

3 个答案:

答案 0 :(得分: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();

        }
    });
}
}

答案 1 :(得分:1)

不需要相反吗?

根据我的理解,您需要从startActivityForResult(..)而不是NavigationActivity致电SearchActivity

NavigationActivity

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

然后在SearchActivity电话

setResult(..);
finish();

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

请参阅此related answer

答案 2 :(得分:0)

startActivityForResult用于将数据从被调用的Activity返回到调用活动,即如果活动A正在调用B,则可以使用startActivityForResult将数据从B返回到A