接收活动的多个意图

时间:2013-08-27 06:44:04

标签: android android-intent

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

活动1:

    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);

活动2:

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 个答案:

答案 0 :(得分:1)

在EnquireActivity onCreate()方法

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

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");
    }
}

答案 1 :(得分: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函数中的数据附加信息,以告知发送方并执行不同的操作。

答案 2 :(得分:0)

您可以使用3种方法:

方法1:您可以在其中使用sharedprefence商店数据并在第3个活动中使用它

方法2:将意图从第1个活动发送到第2个。             在第二个接收那些意图。             然后从第二个活动发送所有先前的意图加上新的意图到第三个活动

方法3:使用捆绑的字符串

答案 3 :(得分: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