0

我必须通过意图将来自两个不同活动的数据发送到同一个活动。从活动1,意图将数据传递给EnquireActivity,从活动2,意图也被传递给EnquireActivity。如何在 EnquireActivity 中接收这些意图。任何帮助,将不胜感激。

活动一:

    Intent i1 = new Intent(this, EnquireActivity.class);
    i1.putExtra("name",et_name.getText().toString());
    i1.putExtra("adults",et_adult.getText().toString());
    i1.putExtra("child",et_child.getText().toString());
    i1.putExtra("email",et_email.getText().toString());
    i1.putExtra("phone",et_phone.getText().toString());
    i1.putExtra("datedept",date1);
    i1.putExtra("datearr",date2);
    i1.putStringArrayListExtra("list1", getChecked);
    startActivity(i1);

活动二:

Intent intent = new Intent(this, EnquireActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("night", n);
        intent.putExtra("day", d);
        intent.putExtra("dest", dest);
        startActivity(intent);
4

4 回答 4

1

在 EnquireActivityonCreate()方法中

从这样的意图中获取额外内容:

Bundle extras = getIntent().getExtras();

if (extras != null) {
    if(extras.contains("child"){
        // that is the intent if from activity1 and contains additional parameters
        name = extras.getString("name");
        datedept = extras.getString("datedept");
        ...


    }
    else{
        intent.putExtra("name", name);
        night = extras.getString("night");
        day = extras.getString("day");
        dest = extras.getString("dest");
    }
}
于 2013-08-27T06:58:22.700 回答
0

要从 EnquireActivity Activity 获取意图,请尝试以下操作:

Intent in = getIntent();
String tv1= in.getExtras().getString("name");
String tv2= in.getExtras().getString("adults");
String tv3= in.getExtras().getString("child");
String tv4= in.getExtras().getString("phone");
......//Like this
于 2013-08-27T07:09:19.080 回答
0

您可以在“onNewIntent()”中处理所有意图

/**
 * This is called for activities that set launchMode to "singleTop" in
 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
 * flag when calling {@link #startActivity}.  In either case, when the
 * activity is re-launched while at the top of the activity stack instead
 * of a new instance of the activity being started, onNewIntent() will be
 * called on the existing instance with the Intent that was used to
 * re-launch it. 
 *  
 * <p>An activity will always be paused before receiving a new intent, so 
 * you can count on {@link #onResume} being called after this method. 
 * 
 * <p>Note that {@link #getIntent} still returns the original Intent.  You 
 * can use {@link #setIntent} to update it to this new Intent. 
 * 
 * @param intent The new intent that was started for the activity. 
 *  
 * @see #getIntent
 * @see #setIntent 
 * @see #onResume 
 */
protected void onNewIntent(Intent intent) {
}

以您的代码为例,您正在向同一个活动发送不同的意图(数据附加值不同)。所以你可以在 onNewIntent 函数中查看额外的数据来告诉发送者并采取不同的操作。

于 2013-08-27T06:45:53.447 回答
0

您可以使用 3 种方法:

方法 1:您可以在其中使用 sharedprefence 存储数据并在您的第三个活动中使用它

方法 2:将意图从第一个活动发送到第二个。在第二次接收这些意图。然后从第二个活动发送所有以前的意图和新的意图到第三个活动

方法 3:使用捆绑字符串

于 2013-08-27T06:47:23.593 回答