Home > Andriod > การส่ง parameter หรือ data ให้กับ activity [Android เก็บเอามาเล่า] ต่อ

การส่ง parameter หรือ data ให้กับ activity [Android เก็บเอามาเล่า] ต่อ

มีเพิ่มเติมต่ออีกในเรื่องของการ ส่งผ่านข้อมูลไปยัง activity (newActivity)อื่นในขณะที่สร้าง activity นั้น ยังมีต่ออีกนิส
ก็เนื่องจากว่า ความต้องการในการใช้งาน ในลักษณะนี้นั้นมี 2 ลักษณะคือ
1 สร้าง activity ที่ 2 แล้วเปิด (Fire and forget) ในกรณีนี้พูดไปแล้วนะครับ ในบทความที่แล้ว
2 สร้าง activity ที่ 2 เปิด แล้วรอ รับค่าส่งกลับ ( First and wait fore response – Ancy callback )
ในบทความนี้เรามาต่อการใช้งานในลักษณะที่ 2
หากเราต้องการ ที่จะ ให้ activity ที่ถูกสร้างขึ้นนั้น นั้นเป็น dialog รับค่าจากผู้ใช้แล้ว ส่งคืนกลับให้กับ activity ที่สร้างมัน (currentActivity) ทำอย่างไร

เรื่องนี้ เรื่องที่ค่อนข้างใช้บ่อยและสำคัญ ครับสำหรับการพัฒนาโปรแกรม เพื่อโต้ตอบกับผู้ใช้ มาดูวิธีการเลยนะครับ
ในการสราง activity แล้วรอ ค่าส่งกลับ เราทำดังนีนะครับ (สมมุติว่า acctivity ปัจจุบันคือ currentAcivity และ activity ที่จะใช้เป็น dialog คือ newActivity)


Intent newIntent = new Intent(this.getApplicationContext(),newActivity.class);
Bundle bundle = new Bundle();
bundle.putString("param1","test");
newIntent.putExras(bundle);
setartActivityForResult(newIntent,REQ_CODE);

** หมายเหตุ – code ด้านบน เขียนใน event อย่างเช่น onClickListener ของ view เป็นต้น

เช่น code เต็ม

   ....
final static int REQ_CODE = 1;
   .....
btnStartAct.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
     Intent newIntent = new Intent(this.getApplicationContext(),newActivity.class);
     Bundle bundle = new Bundle();
     bundle.putString("param1","test");
     newIntent.putExras(bundle);
     setartActivityForResult(newIntent,REQ_CODE );
    });
}

ครับจาก code newActivity เป็น activity ที่เราต้องการสร้าง จาก currentActivity
สำหรับด้านรับข้อมูลด้าน (newActivity)

 Bundle bundle = this.getIntent().getExtras();
 if(bundle != null){
String param1 = bundle.getString("param1");
 }

** ระวังเรื่องการส่งค่านะครับ ในกรณีนี้หากไม่กำหนดค่าให้ param1  ค่าจะเป็น null


ต่อ นะครับ สำหรับการส่งค่ากลับ ให้กลับ currentAcitivity  พร้อมกลับ ส่งการ focus ให้กับ currentActivity ดังนี้

    btnOk.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
      Bundle bundle = new Bundle();
      bundle.putString(“status”, “OK”);

      Intent mIntent = new Intent(); 
      mIntent.putExtras(bundle);

      setResult(RESULT_OK, mIntent);
      finish();
    });
  }

จะเห็นว่าการส่งค่ากลับนั้น ก็ส่งผ่าน Intent โดยใช้ putExtra method แล้วสั่ง finish เลย เป็นการเป็นหน้า activity ไปด้วยเลย สุดท้าย ในด้าน currentActivity ให้เขียน overried method ที่ชื่อว่า  onActivityResult method จะถูกเรียกทันทีหลังจาก newActivity ปิดลงและส่งค่ากลับ ตัวอย่าง code ดังนี้ครับ

@Override protected void onActivityResult(int requestCode,int resultCode,String strdata,Bundle bundle)
{
Log.i(Global.TAG, "MainDriver main-activity got result from sub-activity");
if(resultCode == RESULT_OK)
   {
      // do something here ....
      // รับข้อมูลจาก bundle เหมือนปกติครับ
   }
}

ครับ เป็นอันจบขั้นตอนการ ทำงานทั้งหมดแล้ว หวังว่า จะเป็นประโยชน์ต่อ หลายท่านที่กำลังค้นหาข้อมูลส่วนนี้อยู่นะครับ s_teerapong2000@yahoo.com

Categories: Andriod
  1. อาร์ม
    July 30, 2012 at 10:47 pm

    ขอถาม Intent กับ Noti หน่อยครับคือว่าเป็นกรณีที่ไม่ใช้แค่ 2 หน้า เช่น 3 หน้าคับ
    สมมุติว่าหน้าแรกผมคือหน้า A จะ intent ไปหน้า B แต่ว่า ค่าของหน้า A ผมอยากให้แสดง
    Noti ที่หน้า C คือที่ทำผมลองเอา ค่าของหน้า A และ B มาใส่ที่หน้า C แล้ว แต่ ค่ามันมาแต่ หน้า B ครับ
    พอมีวิธีทำให้หน้า A แสดง Notification ที่หน้า C มัยครับช่วยตอบทีครับ

  2. Teerapong Sontayaman
    July 31, 2012 at 12:41 am

    ครับ ผมเข้าใจว่า คำถามก็คือ การส่งข้อมูลต่อ ต่อ กันไปยัง activity ถัดไป ซึ่งมากกว่า 2 activities ใช่ไหมครับ ก็ขอตอบว่า เราก็ใช้ technique เดิมนั่นแหละครับ เนื่องจากว่า bundle ที่เราใช้ส่งข้อมูลนั้นสามารถที่จะส่งผ่านได้มากกว่า 1 นะครับ ดังนั้นใน activity B ของท่าน ก็สามารถที่จะรวบรวมข้อมูลเพื่อส่งต่อไปยัง activity ที่ต้องการ ได้อีก นะครับ ตัวอย่างของ การใช้ bundle นะครับ

    //Create the intent

    Intent i = new Intent(this.getApplicationContext(),newActivity.class);

    //Create the bundle
    Bundle bundle = new Bundle();

    //เพิ่มข้อมูลที่ต้องการส่ง
    bundle.putString(“stuff”, “foo”);
    bundle.putString(“morestuff”,”foo2”);
    bundle.putInt(“yourMomsAge”, 83);

    // Add bundle ไปยัง intent
    i.putExtras(bundle);

    //Start activity ถัดไป
    startActivity(i);

    วิธิการ อ่านข้อมูลใน Activity ถัดไป

    //Get the bundle
    Bundle bundle = getIntent().getExtras();

    //Extract the data like legs from a turducken…
    if(bundle != null){
    String stuff = bundle.getString(“stuff”); //puts foo in stuff
    int myMomsAge = bundle.getInt(“yourMomsAge”);
    }

    ไม่รู้ว่าตรงประเด็นหรือป่าว ลองดูนะครับ

  3. October 21, 2013 at 10:12 am

    ผมมี 3 activity ผมจะส่งค่าจาก หน้า 3 ไป หน้าที่ 1 โดยไม่เปิดหน้า 1 มาใหม่ได้ั้มั้ยคับ

    ให้ show หน้าแรก (main) และรับค่าจากหน้าที่ 3

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: