StartActivity पर एक बंडल पास करना ()?


174

वर्तमान में लॉन्च की जा रही गतिविधि के लिए एक बंडल पास करने का सही तरीका क्या है? साझा किए गए गुण?

जवाबों:


426

आपके पास कुछ विकल्प हैं:

1) आशय से बंडल का उपयोग करें :

Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);  

2) एक नया बंडल बनाएं

Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);

3) आशय के putExtra () शॉर्टकट विधि का उपयोग करें

Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);


फिर, लॉन्च की गई गतिविधि में, आप उन्हें पढ़ेंगे:

String value = getIntent().getExtras().getString(key)

नोट: बंडलों में सभी आदिम प्रकारों, पार्सलबेल और सीरियलियलज़ के लिए "गेट" और "पुट" विधियां हैं। मैंने सिर्फ स्ट्रिंग्स का इस्तेमाल प्रदर्शनकारी उद्देश्यों के लिए किया है।


20

आप आशय से बंडल का उपयोग कर सकते हैं:

Bundle extras = myIntent.getExtras();
extras.put*(info);

या एक पूरी बंडल:

myIntent.putExtras(myBundle);

यह वही है जो आप खोज रहे हैं?


1
और परिणामी इरादे से आप getIntent ()। GetExtras ()। Get * () प्राप्त करें जिसे पहले संग्रहीत किया गया है।
यानचोको

16

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

6

आप बंडल का उपयोग करके एक गतिविधि से दूसरी गतिविधि तक मानों को पारित कर सकते हैं। अपनी वर्तमान गतिविधि में, एक बंडल बनाएं और विशेष मान के लिए बंडल सेट करें और उस बंडल को इरादे से पास करें।

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

क्यों एक बंडल का उपयोग करते समय आशय वस्तु में गेटएक्स्ट्रा और पुटक्स्ट्रा विधियां हैं?
साइकोसिस404

0

यह लिखें कि आप किस गतिविधि में हैं:

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

यह मेरे लिए काम करता है, आप इसे आज़मा सकते हैं।

स्रोत: https://www.c-sharpcorner.com/article/how-to-send-the-data-one-activity-to-another-activity-in-android-application/


0

आप अपनी पहली गतिविधि में इस कोड का उपयोग कर सकते हैं :

 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"); 
    }
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.