假设我有3项活动。 活动A启动活动B,活动B启动活动C.
在活动A中我有代码
Intent i = new Intent(A.this, B.class);
startActivityForResult(i, SOME_NUMBER);
在活动B中,我有代码:
Intent i = new Intent(B.this, C.class);
finish(); // this activity should die and start the C activity
startActivityForResult(i, SOME_NUMBER);
最后在活动C中我有代码:
setResult(RESULT_OK);
finish();
我的问题是活动A没有收到活动C放置的resultCode。 我在活动A中的onActivityResult(int,int Intent)方法如下所示:
switch(requestCode){
case SOME_NUMBER:
if(resultCode == RESULT_OK){
// the activity C has returned RESULT_OK
}else if(resultCode == RESULT_CANCELED){
// the activity C has returned RESULT_CANCELED
}
break;
}
它始终输入else if(resultCode == RESULT_CANCELED) 我想知道如何将正确的resultCode从C活动传递给A活动。
答案 0 :(得分:1)
完成() - 在您的活动完成后调用此应该关闭。 所以你总是得到Resultcode 0(RESULT_CANCELED = 0)。
示例强>:
<强> first_layout.xml:强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="70px"/>
</LinearLayout>
<强> FirstActivity.java:强>
package com.example.startforresultdemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class FirstActivity extends Activity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
textView = (TextView)this.findViewById(R.id.textView);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("sampleData", "This is Sample Data");
startActivityForResult(intent, 1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==1){
String msg = data.getStringExtra("returnedData");
textView.setText(msg);
}else{
Log.i("babu", "requestCode "+requestCode+" resultCode "+resultCode);
//String msg = data.getStringExtra("returnedData");
textView.setText("requestCode "+requestCode+" resultCode "+resultCode);
}
}
}
<强> SecondActivity.java:强>
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SecondActivity extends Activity{
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intent= getIntent();
String msg = intent.getStringExtra("sampleData");
msg += ", Added at Second";
Intent intent2 = new Intent(this, ThirdActivity.class);
intent2 .putExtra("sampleData", msg);
startActivityForResult(intent2, 10);
//finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==10){
String msg = data.getStringExtra("returnedData");
intent.putExtra("returnedData", msg);
setResult(RESULT_OK, intent);
finish();
}
}
}
<强> ThirdActivity.java:强>
package com.example.startforresultdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class ThirdActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String msg = intent.getStringExtra("sampleData");
msg += ", Added at Third";
intent.putExtra("returnedData", msg);
setResult(RESULT_OK, intent);
finish();
}
}
或按照您的意愿将以下代码添加到您的课程中。您可以从third activity
获得结果。
<强> FirstActivity.java:强>
if(getIntent().getStringExtra("returnedData") != null){
Intent intent= getIntent();
String msg = intent.getStringExtra("returnedData");
textView.setText(msg);
}else{
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("sampleData", "This is Sample Data");
startActivity(intent);
}
SecondActivity.java:
intent= getIntent();
String msg = intent.getStringExtra("sampleData");
msg += ", Added at Second";
Intent intent2 = new Intent(this, ThirdActivity.class);
intent2 .putExtra("sampleData", msg);
finish();
startActivity(intent2);
ThirdActivity.java
Intent intent= getIntent();
String msg = intent.getStringExtra("sampleData");
msg += ", Added at Third";
Intent intent1 = new Intent(this, FirstActivity.class);
intent1.putExtra("returnedData", msg);
startActivity(intent1);
finish();