[英]Calling startActivityForResult never pass to onActivityResult
I'm calling startActivityForResult from a child activity to MainActivity but never pass for onActivityResult 我正在将startActivityForResult从子活动调用为MainActivity,但是从不为onActivityResult传递
secondActivty this is the call 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);
}
And I want see the call in here on the MainActivity I don't care the code I just want to see the call. 我想在MainActivity上看到此呼叫,我不在乎我只想看到该呼叫的代码。
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
System.out.println("HelloWorld");
Log.i(TAG,"HelloWorld");
}
and the manifest 和清单
<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>
onActivityResult()
belongs to your second activity (if its not then it must to), not the MainActivity
. onActivityResult()
属于您的第二个活动(如果不是,则必须属于),而不是MainActivity
。 onActivityResult()
method wont trigger in MainActivity
just cuz MainActivity
is started for result. 只要启动MainActivity
作为结果, onActivityResult()
方法就不会在MainActivity
触发。 Do you realize that? 你知道吗?
Also your MainActivity
if being started for result must SET the result using setResult()
method and than it has to be finished using finish()
method, that way the result will be passed from your MainActivity
to your caller SeconActivity
activity and its onActivityResult()
will be fired. 同样,如果您的MainActivity
为结果而启动,则必须使用setResult()
方法设置结果,然后必须使用finish()
方法finish()
结果,这样,结果将从您的MainActivity
传递给调用方SeconActivity
活动及其onActivityResult()
将被解雇。
您调用的Activity必须调用setResult(Activity.RESULT_OK)
或setResult(Activity.RESULT_OK, data)
MainActivity.java(in onCreate) 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
}
});
(outside onCreate) (在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(in onCreate) 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
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.