0

インテントを介して 2 つの異なるアクティビティから同じアクティビティにデータを送信する必要があります。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

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

次の 3 つのアプローチを使用できます。

アプローチ 1: それらの中で sharedprefence ストア データを使用し、それを 3 番目のアクティビティで使用することができます

アプローチ 2: 1 番目のアクティビティから 2 番目のアクティビティにインテントを送信します。それらのインテントを 2 番目に受け取ります。次に、2 番目のアクティビティから、以前のすべてのインテントと新しいインテントを 3 番目のアクティビティに送信します。

アプローチ 3: バンドルされた文字列を使用する

于 2013-08-27T06:47:23.593 に答える
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

EnquireActivity アクティビティからインテントを取得するには、次を試してください。

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 に答える