Skip to main content

Android Activity

Android Activity


  • Activity life cycle in Hindi
  • Starting an android activity in Hindi
  • Creating a new activity in Hindi
  • Destroying an android activity in Hindi

Introduction to Activity

आप की application में एक से ज्यादा activity हो सकती है। जब आप इन activities के through navigate करते है, यानि एक activity से दूसरी activity में जाते है तो activity अपनी life cycle की different stages में जाती है।

उदाहरण के लिए जब आपकी activity start होती है तो ये foreground में होती है और user का पूरा focus इस पर होता है। इस process के दौरान android system कुछ life cycle methods को call करता है। यदि user कोई दूसरी activity start करता है तो android system उस activity के life cycle methods को call करता है और आपकी पहले वाली activity background में चली जाती है।

life cycle methods में आप decide कर सकते है की जब user जाता है या वापस आता है तो आपकी activity कैसे behave करेगी।  उदाहरण के लिए यदि आप एक वीडियो player बना रहे है तो जब user किसी दूसरी activity पर जाये तो video streaming रुक जानी चाहिए। लेकिन यदि आप एक music player बना रहे है तो आप music को background में भी play कर सकते है. 


Starting An Activity 

जँहा दूसरे programming paradigms में कोई भी एप्लीकेशन main मेथड से स्टार्ट होती है। Android में android system activity को life cycle methods के द्वारा initiate करता है। Activity को start करने के लिए भी methods की sequence होती है और activity को close करने के लिए भी methods के sequence होती है। 

Understanding Activity Life Cycle Methods  

Activity की life cycle के दौरान android system set of methods को call करता है। किसी activity की life cycle एक step pyramid की तरह होती है। Activity life cycle की हर stage separate होती है। जैसे की android system activity को start करता है हर method activity को एक step forward मूव करता है और activity top तक पहुंच जाती है। Activity की top stage में activity running stage में होती है और visible होती है user उसके साथ interact कर सकता है।           

जैसे ही user activity को छोड़ कर जाता है activity की stages pyramid से नीचे की तरफ move करने लग जाती है और last stage में जाके activity destroy हो जाती है। कुछ cases में activity wait भी करती है जैसे की यदि user दूसरी application पर switch कर ले तो activity background में wait करती है। और जब user वापस आता है तो activity फिर से resume हो जाती है।

आप की application की complexity को देखते हुए जरुरी नहीं की आप सभी life cycle methods implement करने  जरुरत हो। लेकिन जरुरी है की आप को सभी methods समझ में आये और जो आपकी एप्लीकेशन के behavior के लिए जरुरी हो आप वो methods implement कर सकते है। Activity की life cycle stages के बारे में कुछ information नीचे दी जा रही है।

  • Created - इस stage में onCreate() मेथड द्वारा activity create की जाती है जब user आपकी application के icon पर क्लिक करता है।   
  • Started - इस stage में  onStart() मेथड द्वारा activity start की जाती है। 
  • Resumed  - इस stage में activity running stage में होती है और user को visible होती है। 
  • Paused  - इस stage में activity paused होती है जब user किसी दूसरी application में switch करता है। 
  • Stopped  - इस stage में activity stopped होती है। 
  • Destroyed - इस stage में activity destroyed हो जाती है।  


Launcher Activity 

जब कोई user आपकी एप्लीकेशन के icon को click करते है तो android system उस activity के onCreate() मेथड को call करता है  जिसे आपने launcher declare किया था। Launcher activity वो activity होती है जो आपकी एप्लीकेशन की main activity होती है। आपकी एप्लीकेशन में कई activities हो सकती है लेकिन सबसे पहले वही activity user को visible होती जिसे आपने launcher या main declare किया है।    

कोनसी activity main activity होगी ये आप AndroidManifest.xml फाइल में declare कर सकते है।  Main activity को <intent-filter > के साथ declare किया जाना चाहिए। जिसमे action MAIN हो और category launcher हो। 

यदि MAIN action या launcher category defined नहीं है तो आपकी application का icon शो नहीं होगा।    

Example 


<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>



Creating a New Activity 

ज्यादातर applications में बहुत सी activities होती है जिनसे user अलग अलग टास्क perform करता है। चाहे activity main activity हो जो user के एप्लीकेशन icon को क्लिक करने से create होती है या कोई दूसरी activity जो आपकी एप्लीकेशन user से interact करते हुए create करती है, android system हर activity को create करने के लिए onCreate() method कॉल करता है। 

आपको onCreate() मेथड को implement करना होता है, इसमें आप application का startup logic परफॉर्म  कर सकते है जो की activity की पूरी life cycle में केवल एक बार ही हो सकता है। उदाहरण के लिए आप अपने implementation में user interface define कर सकते है और कुछ variables भी instantiate कर सकते है। 

जैसे ही onCreate() method का execution खत्म होता है, android system onStart() और onResume() method को कॉल करता है। आपकी activity created और started stages में वापस कभी नहीं आती है। Technically activity onStart() मेथड के कॉल होते ही visible हो जाती है लेकिन इसके बाद onResume() method कॉल होता है जो activity को resume state में ले जाता है activity तब तक resume state  में रहती है जब तक की स्टेट change ना हो जाये। 


Destroying the Activity  

Activity life cycle का पहला मेथड onCreate() होता है और last मेथड onDestroy() होता है। जब Android system आपकी activity पर इस मेथड को कॉल करता है तो ये इस बात का signal होता है की आपकी activity android system की memory से completely remove होने वाली है।

ज्यादातर applications को onDestroy() मेथड को implement करने के जरुरत नहीं होती है क्योंकि सभी local references activity के साथ ही डिलीट हो जाते है। लेकिन यदि आपकी एप्लीकेशन में background threads है तो आपको onDestroy() method को implement करना चाहिए।     

Comments

Popular posts from this blog

Android Services

Android Services Introduction to  android services in Hindi  Declaring  services in Hindi Creating a  android service in Hindi Introduction to Android Services इससे पहले की आप जाने की service क्या होती है आइये जानते है की service क्या नहीं होती है - एक service separate प्रोसेस नहीं होती है। ये आपकी एप्लीकेशन का ही पार्ट होती है।  एक service thread नहीं होती है।  आइये अब देखते है service क्या होती है - Service एक facility होती है जिससे आप android system को बता सकते है की आप कोई काम background में करते रहना चाहते है।  Service एक facility होती है जिससे एक एप्लीकेशन अपनी कुछ functionality दूसरी applications को expose कर सकती है।    हर service क्लास का <service> declaration AndroidManifest.xml file में होना चाहिए। Services को आप context.startService() और context.bindService() मेथड्स से स्टार्ट कर सकते है।            जब service component क्रिएट होता है तो android syst...

Android Spinners

Android Spinners  Introduction to  android spinners in Hindi Defining  android spinners in Hindi Creating spinner items array in Hindi Responding to spinner selections in Hindi    Introduction to Android Spinners Android spinners आपकी एप्लीकेशन में एक drop down menu होती है। इस drop down menu में बहुत से items होते है जिन्हे user choices कहते है। जब user spinner पर क्लिक करता है तो ये items शो होते है। यूज़र अपने लिए appropriate choice choose करता है। By default spinner में सबसे top वाला element शो होता है। Spinners को XML layout file में क्रिएट किया जाता है। और items की लिस्ट string resource file में क्रिएट की जाती है। जिसमे आप एक string array क्रिएट करते है।  Spinner और items को जोड़ने के लिए एक array adapter क्रिएट किया जाता है। इसके बाद selection events को respond करने के लिए OnItemSelectedListener interface को  इम्प्लीमेंट किया जाता है।  Defining Android Spinners Android spinners को आप XML l...