วิธีที่ถูกต้องในการส่งบันเดิลไปยังกิจกรรมที่เริ่มต้นจากบันเดิลปัจจุบันคืออะไร คุณสมบัติที่ใช้ร่วมกัน?
วิธีที่ถูกต้องในการส่งบันเดิลไปยังกิจกรรมที่เริ่มต้นจากบันเดิลปัจจุบันคืออะไร คุณสมบัติที่ใช้ร่วมกัน?
คำตอบ:
คุณมีตัวเลือกน้อย:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  
2) สร้าง Bundle ใหม่
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
3) ใช้วิธีทางลัดputExtra ()ของ Intent
Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
จากนั้นในกิจกรรมที่เปิดตัวคุณจะอ่านผ่าน:
String value = getIntent().getExtras().getString(key)
หมายเหตุ: การรวมกลุ่มมีวิธี "รับ" และ "วาง" สำหรับประเภทดั้งเดิมทั้งหมดพัสดุและ Serializables ฉันใช้ Strings เพื่อจุดประสงค์ในการสาธิต
คุณสามารถใช้ Bundle จากเจตนา:
Bundle extras = myIntent.getExtras();
extras.put*(info);
หรือทั้งชุด:
myIntent.putExtras(myBundle);
นี่คือสิ่งที่คุณกำลังมองหา?
การส่งข้อมูลจากกิจกรรมหนึ่งไปยังกิจกรรมใน Android
เจตนาประกอบด้วยการกระทำและข้อมูลเพิ่มเติมที่เป็นทางเลือก ข้อมูลสามารถส่งผ่านไปยังกิจกรรมอื่น ๆ โดยใช้putExtra()วิธีการตั้งใจ key/value pairsข้อมูลจะถูกส่งเป็นพิเศษและมีความ ที่สำคัญคือสตริงเสมอ เป็นค่าที่คุณสามารถใช้ชนิดข้อมูลดั้งเดิม int, float, chars เป็นต้นนอกจากนี้เรายังสามารถส่งผ่านParceable and Serializableวัตถุจากกิจกรรมหนึ่งไปยังกิจกรรมอื่น ๆ
Intent intent = new Intent(context, YourActivity.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);
การดึงข้อมูลชุดข้อมูลจากกิจกรรม Android
คุณสามารถดึงข้อมูลโดยใช้   getData()วิธีการบนวัตถุเจตนา เจตนาวัตถุสามารถเรียกดูผ่านทางgetIntent()วิธีการ
 Intent intent = getIntent();
  if (null != intent) { //Null Checking
    String StrData= intent.getStringExtra(KEY);
    int NoOfData = intent.getIntExtra(KEY, defaultValue);
    boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
    char charData = intent.getCharExtra(KEY, defaultValue); 
  }
              คุณสามารถส่งผ่านค่าจากกิจกรรมหนึ่งไปยังกิจกรรมอื่นโดยใช้ Bundle ในกิจกรรมปัจจุบันของคุณสร้างบันเดิลและกำหนดบันเดิลสำหรับค่าเฉพาะและส่งบันเดิลนั้นไปยังเจตนา
Intent intent = new Intent(this,NewActivity.class);
Bundle bundle = new Bundle();
bundle.putString(key,value);
intent.putExtras(bundle);
startActivity(intent);
ตอนนี้ใน NewActivity ของคุณคุณสามารถรับชุดข้อมูลนี้และเรียกคืนค่าของคุณ
Bundle bundle = getArguments();
String value = bundle.getString(key);
นอกจากนี้คุณยังสามารถส่งผ่านข้อมูลผ่านเจตนา ในกิจกรรมปัจจุบันของคุณตั้งเจตนาเช่นนี้
Intent intent = new Intent(this,NewActivity.class);
intent.putExtra(key,value);
startActivity(intent);
ตอนนี้ใน NewActivity ของคุณคุณจะได้รับคุณค่าจากความตั้งใจเช่นนี้
String value = getIntent().getExtras().getString(key);
              เขียนนี่เป็นกิจกรรมที่คุณอยู่ใน:
Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
intent.putExtras("string_name","string_to_pass");
startActivity(intent);
ใน NextActivity.java
Intent getIntent = getIntent();
//call a TextView object to set the string to
TextView text = (TextView)findViewById(R.id.textview_id);
text.setText(getIntent.getStringExtra("string_name"));
มันใช้งานได้สำหรับฉันคุณสามารถลอง
คุณสามารถใช้รหัสนี้ในกิจกรรมแรกของคุณ:
 Intent i = new Intent(Context, your second activity.class);
        i.putExtra("key_value", "your object");
        startActivity(i);
และรับวัตถุในกิจกรรมที่สอง :
 Intent in = getIntent();
    Bundle content = in.getExtras();
   // check null
        if (content != null) {
            String content = content_search.getString("key_value"); 
    }