Home Android-Development

PASS DATA FROM MAINACTIVITY OR ANY ACTIVITY TO ANOTHER ACTIVITY



जैसा की मैंने इसमे कहा है, अगर आपको किसी भी Activity से दूसरे Activity में Data Pass(पास) करना हो तो आप इस Bundle Library के सहारे कर सकते हैं।

Bundle एक Android की एक Library जो डाटा(Data) को  एक जगह से दूसरे जगह में भेजने के लिए उसे किया जाता है।

इसमे सबसे पहले हम Bundle को initialize और उसको import करते है हैं, ताकि हम उसको उसे कर सकते हैं। फिर उसके बाद हम, जो भी डाटा भेजना चाहते हैं, उसको Bundle में डालते हैं।

फिर उसके बाद उसको एक "KEY" देते हैं, जिसके द्वारा हम उसको identify(पहचान) और उसकी पहचान कर सकें। 

इसमे सारे डाटा का लगभग फ़ारमैट (Format) देने की कोसिस जो ज्यादा जरूरी था। एक बार bundle में डालने के बाद उसको कहाँ भेजना हैं, वो तय करते हैं।

फिर उसे हम Intent के द्वारा भेज दिया जाता हैं। 


MainActivity:-


Bundle bundle = new Bundle();

Relate link:

bundle.putString("keyStr", "passString");         //For String

bundle.putInt("keyInt", 0);                                  //For Int

bundle.putBoolean("keyBool", true);              // For Boolean

Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);          //another activity can be any activity

intent.putExtras(bundle);

startActivity(intent);





जब हम Intent के द्वारा data दूसरे Activity में भेज देते हैं, तो उसके बाद हम उसे "KEY" के द्वारा get (प्राप्त) करना पड़ता है, फिर हम उसे किसी भी Format Specifier मतलब (String, int, Boolean), जैसे variable में रख सकते हैं। 

और उसको किसी भी जगह पर use कर सकते हैं। जैसा की मैंने इसमे दर्शाया है।



ANOTHER ACTIVITY:-





Bundle bundle = getIntent().getExtras();                    // initialize

Bundle String str = bundle.getString("keyStr");         // get data in the form of

String int intData = bundle.getInt("keyInt");               // get Data in the form of Int

boolean bool = bundle.getBoolean("keyBool");        // get Data in the form of bool





कोई टिप्पणी नहीं

एक टिप्पणी भेजें

Thank You For Comment,

to Top