Activity
您可以使用 将所需结果的“目标”从一个Activity
传递到下一个Intent.FLAG_ACTIVITY_FORWARD_RESULT
。例如,如果您启动ActivityC:
@OnClick(R.id.btn_two)
public void toActivityC(){
Intent intent = new Intent();
intent.setClass(this, ActivityC.class);
intent.setFlag( Intent.FLAG_ACTIVITY_FORWARD_RESULT );
startActivityForResult(intent, 2);
}
这样,新活动可以调用 setResult(int) 并将结果发送回原始活动的回复目标。
(引自Intent
文档)
在您的情况下,这也意味着每个Activity
(B和C)都必须传递迄今为止收集的数据以及Intent
用于启动下一个的数据Activity
。最后,ActivityD可以调用setResult()
将数据传输到ActivityA。
编辑 (1)我正在添加一些代码来演示如果用户按下 BACK 键如何做出反应。为简单起见,我们假设用户只决定在ActivityD中按 BACK 。
编辑(2) 请注意,Intent.putExtra()
可以采用Bundle
,因此您可以将Bundle
所有数据一起传递给一个整体,而不是将所有值一个一个地传输到新的Intent
. 使用一个Bundle
额外的而不是其他几个额外的可以减少代码行数。
xml 文件
活动_a.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="Results:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/tv_result1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="---"/>
<TextView
android:id="@+id/tv_result2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="---"/>
<TextView
android:id="@+id/tv_result3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="---"/>
<Button
android:id="@+id/btn_get_results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get results!"/>
</LinearLayout>
活动_b.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="The following text will be sent as result:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:text="Forwarding Results"/>
<Button
android:id="@+id/btn_get_results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get more results!"/>
</LinearLayout>
活动_c.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="The following text will be sent as result:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff00ff"
android:text="is"
android:checked="true"
android:id="@+id/cb_result"/>
<Button
android:id="@+id/btn_get_results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get more results!"/>
</LinearLayout>
活动_d.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="The following text will be sent as result:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ff"/>
<Button
android:id="@+id/btn_send_results"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send all results!"/>
</LinearLayout>
活动
活动A
public class ActivityA extends AppCompatActivity
{
static final int REQUEST_CODE = 42;
static final String KEY_RESULT_FROM_B = "result_b";
static final String KEY_RESULT_FROM_C = "result_c";
static final String KEY_RESULT_FROM_D = "result_d";
private TextView tvResult1, tvResult2, tvResult3;
private Button btnGetResults;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
tvResult1 = (TextView) findViewById(R.id.tv_result1);
tvResult2 = (TextView) findViewById(R.id.tv_result2);
tvResult3 = (TextView) findViewById(R.id.tv_result3);
btnGetResults = (Button) findViewById(R.id.btn_get_results);
btnGetResults.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
startActivityForResult(new Intent(ActivityA.this, ActivityB.class), REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (REQUEST_CODE == requestCode)
{
if (resultCode == RESULT_OK)
{
tvResult1.setText(data.getStringExtra(KEY_RESULT_FROM_B));
tvResult2.setText(data.getStringExtra(KEY_RESULT_FROM_C));
tvResult3.setText(data.getStringExtra(KEY_RESULT_FROM_D));
btnGetResults.setEnabled(false);
}
else
{
Toast.makeText(ActivityA.this, "no result", Toast.LENGTH_SHORT).show();
// do something else
}
}
}
}
活动B
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
TextView tvResult = (TextView) findViewById(R.id.tv_result);
final String result = tvResult.getText().toString();
Button btnGetResults = (Button) findViewById(R.id.btn_get_results);
btnGetResults.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(ActivityB.this, ActivityC.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.putExtra(ActivityA.KEY_RESULT_FROM_B, result);
startActivity(intent);
finish();
}
});
}
活动C
public class ActivityC extends AppCompatActivity
{
static final String IS ="is";
static final String IS_NOT = "is not";
private CheckBox cbResult;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
cbResult = (CheckBox) findViewById(R.id.cb_result);
cbResult.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
buttonView.setText(isChecked ? IS : IS_NOT);
}
});
String previousResult = getIntent().getStringExtra(ActivityA.KEY_RESULT_FROM_C);
cbResult.setChecked( ! IS_NOT.equals(previousResult) );
final String result1 = getIntent().getStringExtra(ActivityA.KEY_RESULT_FROM_B);
Button btnGetResults = (Button) findViewById(R.id.btn_get_results);
btnGetResults.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(ActivityC.this, ActivityD.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.putExtra(ActivityA.KEY_RESULT_FROM_B, result1);
String result2= cbResult.getText().toString();
intent.putExtra(ActivityA.KEY_RESULT_FROM_C, result2);
startActivity(intent);
finish();
}
});
}
}
活动D
public class ActivityD extends AppCompatActivity
{
static final String POSSIBLE = "possible :)";
static final String COMPLICATED = "complicated";
private String result1, result2;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_d);
tvResult = (TextView) findViewById(R.id.tv_result);
result1 = getIntent().getStringExtra(ActivityA.KEY_RESULT_FROM_B);
result2 = getIntent().getStringExtra(ActivityA.KEY_RESULT_FROM_C);
if (ActivityC.IS.equals(result2))
{
tvResult.setText(POSSIBLE);
}
else
{
tvResult.setText(COMPLICATED);
}
Button btnSendResults = (Button) findViewById(R.id.btn_send_results);
btnSendResults.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.putExtra(ActivityA.KEY_RESULT_FROM_B, result1);
intent.putExtra(ActivityA.KEY_RESULT_FROM_C, result2);
intent.putExtra(ActivityA.KEY_RESULT_FROM_D, tvResult.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
});
}
@Override
public void onBackPressed()
{
// start ActivityC once more
Intent intent = new Intent(ActivityD.this, ActivityC.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
intent.putExtra(ActivityA.KEY_RESULT_FROM_B, result1);
// pass this to set the CheckBox like the user left it:
intent.putExtra(ActivityA.KEY_RESULT_FROM_C, result2);
startActivity(intent);
finish();
}
}