我从子活动到MainActivity调用startActivityForResult但从未传递onActivityResult
secondActivty这是电话
private void bringMainActivityToTop() {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bun= new Bundle();
bun.putParcelable("value",new ListData("HelloDummy",longi,lati,"data"));
intent.putExtra("bundle",bun);
setResult(RESULT_OK, intent);
startActivityForResult(intent, 111);
}
我希望在MainActivity上看到这里的电话我不在乎我只想看电话的代码。
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
System.out.println("HelloWorld");
Log.i(TAG,"HelloWorld");
}
和清单
<activity
android:name="com.test.mppqvat.activity.MainActivity"
android:label="@string/main_act_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
答案 0 :(得分:1)
onActivityResult()
属于您的第二项活动(如果不是必须的话),而不是MainActivity
。 onActivityResult()
方法不会触发MainActivity
方法只是因为MainActivity
因为结果而启动MainActivity
。你意识到了吗?
您的setResult()
如果为结果启动,则必须使用finish()
方法设置结果,而不是必须使用MainActivity
方法完成,这样结果将从您的{{1对您的来电者SeconActivity
活动及其onActivityResult()
将被解雇。
答案 1 :(得分:0)
您的被叫活动必须致电setResult(Activity.RESULT_OK)
或setResult(Activity.RESULT_OK, data)
答案 2 :(得分:0)
MainActivity.java(在onCreate中)
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
(在onCreate之外)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
//use resulted message
}
}
SecondActivity.java(在onCreate中)
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
}
});