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 を押すことにしたと仮定しましょう。
EDIT (2)Intent.putExtra()は を受け取ることができるので、すべての値を 1 つずつ新しい に転送する代わりに、すべてのデータとともにBundle全体を渡すことができ ます。他のいくつかのエクストラではなく1 つのエクストラを使用すると、コードの行数を減らすことができます。BundleIntentBundle
xml ファイル
activity_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>
activity_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>
activity_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>
activity_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();
}
}