从多个活动向单个活动发送额外内容

时间:2016-04-18 10:31:29

标签: java android android-activity

我有一组活动,如A,B,C,D。如果A是家庭活动,我想通过将Extras发送到不同活动的活动A来发送值。

来自B:

Intent intent = new Intent(B.this, A.class);
intent.putExtra("from_B", true);
startActivity(intent)

来自C:

Intent intent = new Intent(C.this, A.class);
intent.putExtra("from_C", true);
startActivity(intent)

来自D:

Intent intent = new Intent(D.this, A.class);
intent.putExtra("from_D", true);
startActivity(intent)

在活动A:

if (getIntent().getExtras() != null) {
        isCreate = (boolean) getIntent().getSerializableExtra("-----");
    }

有人可以帮助我在活动A中保留(------)的位置,以便确定它所称的活动

4 个答案:

答案 0 :(得分:6)

在活动A中,声明一个字符串标记:

public static final String    TAG_ACTIVITY_FROM = "WhichActivity";

在其他活动中,发送:

Intent intent = new Intent(D.this, A.class);
intent.putExtra(A.TAG_ACTIVITY_FROM, "From_D");
startActivity(intent);

在A中,用以下内容取回:

if (getIntent().getExtras() != null) {
    String activityFrom = (String) getIntent().getStringExtra(TAG_ACTIVITY_FROM);
}

编辑: 您可以根据需要添加多个额外内容,例如(来自活动D)

Intent intent = new Intent(D.this, A.class);
intent.putExtra(A.TAG_ACTIVITY_FROM, "From_D");
intent.putExtra(A.VALUE, 4);
startActivity(intent);

在活动A中:

public static final String VALUE = "value";

if (getIntent().getExtras() != null) {
  String activityFrom = getIntent().getStringExtra(TAG_ACTIVITY_FROM);
  int value = getIntent().getIntExtra(VALUE, -1);
}

答案 1 :(得分:0)

使用startActivityForResult

private static final int B = 1;
private static final int C = 2;
private static final int D = 3;

Intent intent = new Intent(context, ActivityB.class);
                startActivityForResult(intent, B);

或:

Intent intent = new Intent(context, ActivityC.class);
                startActivityForResult(intent, C);

或:

Intent intent = new Intent(context, ActivityD.class);
                startActivityForResult(intent, D);


 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK)
        {
            if (requestCode == B)
            {
                boolean b = (boolean)data.getExtras().get("b");
            }
            if (requestCode == C)
            {
                boolean c = (boolean)data.getExtras().get("c");
            }
            if (requestCode == D)
            {
                boolean d = (boolean)data.getExtras().get("d");
            }
        }
    }

并在您的B或C或D活动中:

这是B活动的样本

Intent intent = getIntent();
intent.putExtra("b",true);
setResult(RESULT_OK,intent);
finish();

答案 2 :(得分:0)

来自B:

Intent intent = new Intent(B.this, A.class);
intent.putExtra("NAVIGATION_TYPE", "Activity B");
startActivity(intent)

来自C:

Intent intent = new Intent(C.this, A.class);
intent.putExtra("NAVIGATION_TYPE", "Activity C");
startActivity(intent)

来自D:

Intent intent = new Intent(D.this, A.class);
intent.putExtra("NAVIGATION_TYPE", "Activity D");
startActivity(intent)

在活动A:

if (getIntent().getExtras() != null) {
     String cmgString= getIntent().getStringExtra("NAVIGATION_TYPE");
    }

如果您想直接发送整数值,并使用getIntExtra接收它。

答案 3 :(得分:-1)

将一个额外参数传递给活动并进入A

 Intent intent = new Intent(D.this, A.class);
    intent.putExtra("from_B", true);
    intent.putExtra("activity", "A");
    startActivity(intent);
第二个活动中的

  if (getIntent().getExtras() != null) {
        String from = (String)getIntent().getStringExtra("from_B");
        String fromActivity = (String)getIntent().getStringExtra("activity");
    }