You are on page 1of 15

‫‪Activities‬‬

‫‪• An Android activity is a GUI component (contains the Ul of an application).‬‬


‫‪• Main purpose is to interact with the user.‬‬
‫‪• An application can have zero or more activities.‬‬
‫‪• It corresponds roughly to a window in a desktop application.‬‬
‫‪• Since mobile phone screens are small, an activity typically takes up the whole‬‬
‫‪screen.‬‬
‫‪• If you open multiple activities they are stacked on top of each other.‬‬
‫‪• You cannot arrange activities side by side, like you can do with desktop windows.‬‬
‫‪• Applications must designate one activity as their main task or entry point.‬‬
‫‪That activity is the first to be executed when the app is launched.‬‬
‫‪• An activity may transfer control and data to another activity through‬‬
‫‪an inter-process communication protocol called intents.‬‬

‫‪For example, a login activity may show a screen to enter user name and password.‬‬
‫‪After clicking a button some authentication process is applied on the data, and before‬‬
‫‪the login activity ends some other activity is called.‬‬

‫• النشاط بال ‪ Android‬هو أحد مكونات واجهة المستخدم الرسومية (يحتوي على ‪ Ul‬لتطبيق ما)‪.‬‬
‫(شرح ال‪ activity‬هي كل شاشة خاصة بالتطبيق تظهر على كامل الشاشة (عند فتح التطبيق) مع كافة‬
‫المكونات التي تحتويها )‬

‫الغرض الرئيسي هو التفاعل مع المستخدم‪.‬‬ ‫•‬


‫يمكن أن يحتوي التطبيق على صفر أو أكثر من األنشطة‪( .‬ممكن تطبيق اول ما اكبس عليه يتصل‬ ‫•‬
‫بداتا بيس ويحذف الجداول ويطلع من دون ما يطلع أيا شاشة (‪)activity‬‬
‫إنه يتوافق تقريبًا مع نافذة في تطبيق سطح المكتب‪( .‬القصد يشبه النوافذ عند انشاء تطبيقات للويندوز‬ ‫•‬
‫بال ‪) gui‬‬

‫نظرً ا ألن شاشات الهاتف المحمول صغيرة ‪ ،‬فإن النشاط عادة ما يشغل الشاشة بأكملها‪.‬‬ ‫•‬
‫إذا فتحت أنشطة متعددة ‪ ،‬فسيتم تكديسها فوق بعضها البعض‪( .‬وضعها بال ‪) Activity Stack‬‬ ‫•‬
‫ال يمكنك ترتيب األنشطة جنبًا إلى جنب ‪ ،‬كما تفعل مع نوافذ سطح المكتب‪.‬‬ ‫•‬

‫يجب أن تحدد التطبيقات نشا ًطا واحدًا كمهمة رئيسية (متل فانكشن ال ‪ )main‬أو نقطة دخول‪.‬‬ ‫•‬
‫الن هذا النشاط هو أول نشاط يتم تنفيذه عند تشغيل التطبيق‪.‬‬

‫قد يقوم النشاط بنقل التحكم والبيانات إلى نشاط آخر من خالل بروتوكول اتصال بين العمليات يسمى‬ ‫•‬
‫(النوايا ) (هذه الترجمة الحرفية اما الحقيقية هي ((( الجسور )))‪.‬‬

‫على سبيل المثال ‪ ،‬قد يُظهر نشاط تسجيل الدخول شاشة إلدخال اسم المستخدم وكلمة المرور‪ .‬بعد النقر‬
‫فوق الزر ‪ ،‬يتم تطبيق بعض عمليات المصادقة على البيانات ‪ ،‬وقبل انتهاء نشاط تسجيل الدخول ‪ ،‬يتم‬
‫استدعاء بعض األنشطة األخرى‪( .‬أي الذهاب لصفحة أخرى مثال )‬

‫مالحظة هامة اخر ساليدين هامينننننننن جدا جدا جدا‬


‫دورة حياة المكونات ‪Component's Life Cycle‬‬
‫‪Life and Death in Android‬‬

‫‪Each Android application runs inside its own instance of a Virtual Machine‬‬
‫‪(VM).‬‬

‫‪At any point in time several parallel VM instances could be active (real‬‬
‫)‪parallelism as opposed to task-switching‬‬

‫‪Unlike a common Windows or Unix process, an Android application does not‬‬


‫‪completely control the completion of its lifecycle.‬‬

‫‪Occasionally hardware resources may become critically low and the OS could order‬‬
‫‪early termination of any process. The decision considers factors such as:‬‬

‫‪1. Number and age of the application's components currently running,‬‬


‫‪2. relative importance of those components to the user, and‬‬
‫‪3. how much free memory is available in the system.‬‬

‫مالحظة هامة سوف اعيد جلب تعريف مصطلح ال ‪ instance‬من المحاضرة األولى ألهميته‬
‫إلزالة االلتباس عن هذا المصطلح سوف اذكر تعريفه باإلنكليزية والعربية (افضل ترجمة له الحالة )‬

‫•‬ ‫‪instance variable = variable that created into class and can be used‬‬
‫‪by all methods of class‬‬
‫•‬ ‫‪Instance variables are variables which are bound to class instances‬‬
‫)‪(objects‬‬
‫هو متغير مرتبط بالصف الذي انشأنا منه ‪ obj‬ويمكن استخدامه عبر كل الفانكشن بهذا الصف‬ ‫•‬
‫مثال عندنا صف اسمه ‪ School‬وبه متغير ‪ instance‬اسمه ‪teacher‬‬ ‫•‬
‫وقمت بانشاء ‪ obj 2‬من الصف ‪ School‬األول ‪ school1‬والثاني ‪school2‬‬ ‫•‬
‫في كل واحد من هذه االوبجيكت يوجد متغير خاص به منفصل تماما عن غيره‬ ‫•‬
‫ومدة وجوده مرتبطة بمدة وجود االوبجيكت الذي انشأه‬ ‫•‬

‫الحياة والموت في ‪Android‬‬


‫• يعمل كل تطبيق ‪ Android‬داخل ال (‪ (instance vm‬الخاص به من الجهاز االفتراضي‬
‫‪)VM).‬‬

‫في أي وقت من األوقات ‪ ،‬يمكن أن تكون عدة ‪ VM instance‬متوازية نشطة (تعمل على التفرع)‬ ‫•‬
‫(التوازي الحقيقي بدالً من تبديل المهام (‪) )task-switching‬‬

‫على عكس عملية ‪ Windows‬أو ‪ Unix‬الشائعة ‪ ،‬ال يتحكم تطبيق ‪ Android‬تمامًا في إكمال‬ ‫•‬
‫دورة حياته‪( .‬المعنى قد يجبر النظام تطبيق على التوقف تماما عند حاجته للموارد )‬

‫في بعض األحيان قد تصبح موارد األجهزة منخفضة للغاية ويمكن لنظام التشغيل أن يأمر بإنهاء مبكر‬
‫ألي عملية‪ .‬يأخذ القرار بعين االعتبار عوامل مثل‪( :‬هام جدا ماهي عوامل إيقاف تطبيق )‬

‫‪ .1‬عدد وعمر مكونات التطبيق قيد التشغيل حال ًيا ‪،‬‬


‫‪ .2‬األهمية النسبية لتلك المكونات للمستخدم ‪ ،‬و‬
‫‪ .3‬مقدار الذاكرة الخالية المتوفرة في النظام‪.‬‬
Life and Death in Android
Component's Life Cycle

All components execute according to a master plan that consists of:

1. A beginning - responding to a request to instantiate them

2. An end - when the instances are destroyed.

3. A sequence of in-between states - components sometimes are active or inactive,


or in the case of activities - visible or invisible.

Life as an Android Application:


Start Active / Inactive ►End
Visible / Invisible

:‫يتم تنفيذ جميع المكونات وف ًقا لخطة رئيسية تتكون من‬

‫ االستجابة لطلب إنشاء حالة (بداية ) لها‬- ‫بداية‬

.‫ عندما يتم تدمير الحاالت‬- ‫نهاية‬

‫ أو في عند النشاط‬، ‫ تكون المكونات أحيا ًنا نشطة أو غير نشطة‬- ‫سلسلة من الحاالت البينية‬
.‫ مرئية أو غير مرئية‬- ‫قد يكون احدى الحالتين‬
The Activity Stack Component's Life Cycle

• Activities in the system are scheduled using an activity stack.

• When a new activity is started, it is placed on top of the stack to become


the running activity

• The previous activity is pushed-down one level in the stack, and may come
back to the foreground once the new activity finishes.
<77)

• If the user presses the Back Button the current activity is


terminated and the previous activity on the stack moves up to become active.

• Android 4.0 introduced the 'Recent app’ button to arbitrarily pick as 'next'
any entry currently in the stack (more on this issue later)

Virtual buttons (Android 4.x and 5.x): Back, Home, Recent apps
‫مكدس النشاط‬
.‫يتم جدولة األنشطة في النظام باستخدام مكدس األنشطة‬

) ‫ يتم وضعه أعلى المكدس ليصبح النشاط الجاري (الذي يعمل‬، ‫عند بدء نشاط جديد‬

‫ وقد يعود إلى المقدمة‬، ‫يتم دفع النشاط السابق لألسفل بمقدار مستوى واحد في المكدس‬
.‫بمجرد انتهاء النشاط الجديد‬

‫ يتم إنهاء النشاط الحالي ويتحرك النشاط السابق‬، ‫إذا ضغط المستخدم على زر الرجوع‬
ً
.‫نشطا‬ ‫على المكدس ألعلى ليصبح‬

‫ زر "التطبيق األخير" الختيار "التالي" بشكل تعسفي أي إدخال موجود‬Android 4.0 ‫قدم‬
)‫حاليًا في المكدس (المزيد حول هذه المشكلة الح ًقا‬
) ‫(الشرح هذا الزر يختار أي التطبيقات تصبح بالمقدمة عوضا عن االلتزام بدور المكدس‬
Life Cycle Callbacks Component's Life Cycle

When progressing from one state to the other, the OS notifies the application of the
changes by issuing calls to the following protected transition methods:

void onCreate()
void onPause()
void onStart()
void onStop()
void onRestart()
void onDestroy()
void onResume()

‫عمليات إعادة االتصال الخاصة بدورة الحياة‬

‫ يقوم نظام التشغيل بإخطار (اشعار او اخبار ) التطبيق بالتغييرات عن‬، ‫عند التقدم من حالة إلى أخرى‬
) ‫ (الفانكشنات هامين جدا‬:‫) إلى طرق النقل المحمية التالية‬issuing calls( ‫طريق إصدار مكالمات‬

Creating Activities
• To create an activity, a Java class that extends the Activity base class is
created.
import android.app.Activity;
•••

public class ActivitylOlActivity extends Activity {


super.onCreate(savedlnstanceState);
setContentView(R.layout.activity_main); }

• The activity class loads it Ul component using the XML file defined in the
res/layout folder.
setContentView(R.Layout.activity_main};

‫ داخل المسار‬xml ‫ ومكوناته تكون في ملف‬Activity ‫إلنشاء اكتيفتي جديدة نقوم بالوراثة من الصف‬
res /layout

Declaring Activities
• Each activity in the application, must be declared in the
AndroidManifest.xml file. ‫كل نشاط جديد يجب ان يتم تعريفه‬
‫داخل ملف اسمه‬
<activity AndroidManifest.xml
android:label="@string/app_name"
android:name=".Activityl01Activity">
< intent-f ilterX/intent-f ilter>
</activity>

Activity Life Cycle:


the stages an activity goes through from the moment it appears on the
screen to the moment it's hidden.
.‫المراحل التي يمر بها النشاط من لحظة ظهوره على الشاشة إلى لحظة إخفائه‬: ‫تعريف اخر هام‬
‫‪Life Cycle: Activity States and Callback Methods‬‬
‫‪An activity has essentially three phases:‬‬
‫‪1. It is active or running‬‬ ‫يتكون النشاط بشكل أساسي من ثالث مراحل‪:‬‬
‫‪2. It is paused or‬‬ ‫( هام جدا جدا جدا سؤال مثال ما الفانكشن التي تعمل بين حالتي‬
‫التوقف المؤقت والعمل او اختر الترتيب الصحيح للفانكشن او‬
‫‪3. It is stopped .‬‬
‫ماذا يحصل عند عمل هذه الفانكشن الخ الخ )‬
‫‪Moving from one state to the other is accomplished by means of the callback‬‬
‫‪methods listed on the edges of the diagram.‬‬

‫يتم االنتقال من حالة إلى أخرى عن طريق (طرق رد االتصال) المدرجة على حواف الرسم التخطيطي‪.‬‬
Component's Life Cycle
Activity State: RUNNING

1. Your activity is active or running when it is in the foreground


of the screen (seating on top of the activity stack).

This is the activity that has "focus" and its graphical


interface is responsive to the user's interactions.
ً
.)‫نشطا أو قيد التشغيل عندما يكون في مقدمة الشاشة (الجلوس أعلى كومة األنشطة‬ ‫ يكون نشاطك‬. •

.‫هذا هو النشاط الذي "يركز" وواجهته الرسومية تستجيب لتفاعالت المستخدم‬ •


Component's Life Cycle
Activity State: PAUSED
2. Your Activity is paused if it has lost focus but is still visible to the user.

That is, another activity seats on top of it and that new activity either is
transparent or doesn't cover the full screen.

A paused activity is alive (maintaining its state information and attachment to


the window manager).

Paused activities can be killed by the system when available memory becomes
extremely low.
.‫ يتم إيقاف نشاطك مؤق ًتا إذا فقد التركيز ولكنه ال يزال مرئ ًيا للمستخدم‬.2

ً
‫نشاطا آخر يجلس فوقه وهذا النشاط الجديد إما أن يكون شفا ًفا أو ال يغطي ملء‬ ‫وهذا يعني أن هناك‬
.‫الشاشة‬

.)‫النشاط المتوقف مؤق ًتا على قيد الحياة (الحفاظ على معلومات حالته ومرفقه بمدير النافذة‬

.‫يمكن للنظام إيقاف األنشطة المتوقفة مؤق ًتا عندما تصبح الذاكرة المتاحة منخفضة للغاية‬
Component's Life Cycle
Activity State: STOPPED
3. Your Activity is stopped if it is completely obscured by another activity.

Although stopped, it continues to retain all its state information.

It is no longer visible to the user ( its window is hidden and its life cycle could be
terminated at any point by the system if the resources that it holds are needed
elsewhere).
.‫ يتم إيقاف نشاطك إذا تم حجبه تما ًما بواسطة نشاط آخر‬.3

.‫ إال أنه يواصل االحتفاظ بجميع معلومات الحالة الخاصة به‬، ‫على الرغم من توقفه‬

‫لم يعد مرئ ًيا للمستخدم (تم إخفاء نافذته ويمكن للنظام إنهاء دورة حياته في أي وقت إذا كانت هناك حاجة‬
.)‫إلى الموارد التي يحتوي عليها في مكان آخر‬
Activity Life Cycle

• The activity class defines the following events:


• onCreate ()—Called when the activity is first created
• onStart ()—Called when the activity becomes visible to the user
• onResume ()—Called when the activity starts interacting with the user
• onPause ()—Called when the current activity is being paused and the previous activity is
being resumed
• onStop () —Called when the activity is no longer visible to the user
• onDestroyO—Called before the activity is destroyed by the system (either manually or by
the system to conserve memory)
• onRestart ()—Called when the activity has been stopped and is restarting again
:‫تحدد فئة النشاط األحداث التالية‬
‫ يتم استدعاؤه عند إنشاء النشاط ألول مرة‬onCreate () -

‫ يتم استدعاؤه عندما يصبح النشاط مرئيًا للمستخدم‬onStart () -

‫ يتم استدعاؤه عندما يبدأ النشاط بالتفاعل مع المستخدم‬onResume () -

‫ يتم االتصال به عندما يتم إيقاف النشاط الحالي مؤق ًتا ويتم استئناف النشاط السابق‬onPause () -

‫ يتم استدعاؤه عندما ال يكون النشاط مرئيًا للمستخدم‬onStop () -

‫ يتم االستدعاء قبل تدمير النشاط بواسطة النظام (إما يدويًا أو بواسطة النظام للحفاظ‬onDestroyO -
)‫على الذاكرة‬

‫ يتم االتصال به عند توقف النشاط وإعادة التشغيل مرة أخرى‬onRestart () -


‫هام جدا مالحظة االختالفات في حالة قدوم نشاط جديد او قدوم نشاط قديم اما الحالي‬
Life Cycle Callbacks
Application's Life Cycle

Foreground Lifetime ‫عمر المقدمة‬

• An activity begins its lifecycle when it enters the onCreate() state.

• If it is not interrupted or dismissed, the activity performs its job and finally terminates
and releases resources when reaching the onDestroy() event.

onCreate (). ‫• يبدأ النشاط دورة حياته عندما يدخل الحالة‬


‫ فإن النشاط يؤدي وظيفته وينهي الموارد ويطلقها في النهاية عند الوصول‬، ‫• إذا لم تتم مقاطعته أو رفضه‬
onDestroy (). ‫إلى حدث‬

Application's Life Cycle


Associating Lifecycle Events with Application's Code
Applications do not need to implement each of the transition methods,
however there are mandatory and recommended states to consider

(Mandatory)
onCreate() must be implemented by each activity to do its initial setup.
The method is executed onlyonce on the activity's lifetime.

(Highly Recommended)
onPause() should be implemented whenever the application has some
important data to be committed so it could be reused.

‫ربط أحداث دورة الحياة بكود التطبيق‬

‫ ولكن هناك حاالت إلزامية وموصى بها‬، ‫ال تحتاج التطبيقات إلى تنفيذ كل طريقة من طرق االنتقال‬
‫للنظر فيها‬

)‫(إلزامي‬
.‫ بواسطة كل نشاط للقيام بإعداده األولي‬onCreate () ‫يجب تنفيذ‬
.‫يتم تنفيذ الطريقة مرة واحدة فقط في عمر النشاط‬

)‫(ينصح به بشده‬
‫ كلما كان لدى التطبيق بعض البيانات المهمة ليتم االلتزام بها حتى يمكن‬onPause () ‫يجب تنفيذ‬
.‫إعادة استخدامها‬
Application's Life Cycle

Associating Lifecycle Events with Application's Code


onCreate()

• This is the first callback method to be executed when an activity is created.

• Most of your application's code is written here.

• Typically used to initialize the application's data structures, wire-up Ul view


elements (buttons, text boxes, lists) with local Java controls, define listeners'
behavior, etc.

• It may receive a data Bundle object containing the activity's previous state (if any).

• Followed by onStart() —> onResume()....


‫ربط أحداث دورة الحياة بكود التطبيق‬ •
)( ‫عند اإلنشاء‬ •

.‫هذا هو أول أسلوب رد نداء يتم تنفيذه عند إنشاء نشاط‬ •

.‫معظم كود التطبيق الخاص بك مكتوب هنا‬ •

‫ ومربعات النص‬، ‫المتصلة (األزرار‬Ul ‫ وعناصر عرض‬، ‫ُتستخدم عاد ًة لتهيئة هياكل بيانات التطبيق‬ •
.‫ وما إلى ذلك‬، ‫ وتحديد سلوك المستمعين‬، ‫ المحلية‬Java ‫ والقوائم) باستخدام عناصر تحكم‬،

.)‫قد يتلقى عنصر حزمة بيانات يحتوي على الحالة السابقة للنشاط (إن وجدت‬ •
onStart () -> onResume () .... ‫متبو ًعا بـ‬ •
onPause()
1. Called when the system is about to transfer control to another activity. It should be
used to safely write uncommitted data and stop any work in progress.

2. The next activity waits until completion of this state.

3. Followed either by onResume() if the activity returns back to the foreground, or by


onStop() if it becomes invisible to the user.
4. A paused activity could be killed by the system.
.‫يتم االتصال به عندما يكون النظام على وشك نقل التحكم إلى نشاط آخر‬
.‫يجب استخدامه لكتابة البيانات غير الملتزم بها بأمان وإيقاف أي عمل قيد التقدم‬

.‫ ينتظر النشاط التالي حتى اكتمال هذه الحالة‬.2

، ‫ إذا عاد النشاط إلى المقدمة‬onResume () ‫ يتبعها إما‬.3


.‫ إذا أصبح غير مرئي للمستخدم‬onStop () ‫أو عن طريق‬

.‫ يمكن أن يقتل النظام النشاط المتوقف مؤق ًتا‬.4


Application's Life Cycle
Killable States
Android OS may terminate a killable app whenever the resources needed to run other
operation of higher importance are critically low.

When an activity reaches the methods: onPause(), onStop( ), and onDestroy()it


becomes killable.

onPause () is the only state that is guaranteed to be given a chance to complete


before the process is terminated.

You should use onPause() to write any pending persistent data.

‫ تطبي ًقا قابالً للقتل عندما تكون الموارد الالزمة لتشغيل عملية أخرى‬Android ‫قد ينهي نظام التشغيل‬
.‫ذات أهمية أعلى منخفضة للغاية‬

.‫ يصبح قابال للقتل‬onDestroy () ‫ و‬onStop () ‫ و‬onPause () :‫عندما يصل نشاط إلى التوابع‬

.‫ هي الحالة الوحيدة المضمونة لمنحها فرصة إلكمالها قبل إنهاء العملية‬onPause ()

.‫ لكتابة أي بيانات معلقة معلقة‬onPause () ‫يجب عليك استخدام‬

Example

Note Even if an application has only one activity and the activity is killed, the
application is still running in memory.

‫ فسيظل التطبيق قيد‬، ‫مالحظة حتى إذا كان التطبيق يحتوي على نشاط واحد فقط وتوقف النشاط‬ •
.‫التشغيل في الذاكرة‬

‫مالحظة هامة هذا المثال والساليد الذي بعده هام جدا جدا جدا‬ •
Example

When the activity is first loaded, lifecycle Step: onCreate)) - onStart() –


onResume))

If you click the Back button on the Android emulator, lifecycle Step:
onPause)) – onStop)( - onDestroy()

Click the Home button, click the Overview icon, select the application,
lifecycle Step: onCreate)) – onStart)) – onResume))

Click the Home button and then click the Phone button on the Android
emulator so that the activity is pushed to the background, lifecycle
Step: onPause() – onStop))

Notice that the onDestroy)) event is not called, indicating that the
activity is still in memory. Exit the phone dialer by clicking the Back
button. The activity is now visible again, lifecycle Step: onRestart) (-
onStart)( – onResume))

، ‫• عند تحميل النشاط ألول مرة‬


onCreate () - onStart () - onResume () :‫خطوة دورة الحياة‬

،Android ‫• إذا قمت بالنقر فوق الزر "رجوع" في محاكي‬


onPause () - onStop () - onDestroy () :‫خطوة دورة الحياة‬

‫ ودورة‬، ‫ وحدد التطبيق‬، ‫ وانقر فوق أيقونة نظرة عامة‬، ‫• انقر فوق زر الصفحة الرئيسية‬
onCreate () - onStart () - onResume () :‫الحياة الخطوة‬

‫ بحيث يتم‬Android ‫• انقر فوق زر الصفحة الرئيسية ثم انقر فوق زر الهاتف في محاكي‬
‫دفع النشاط إلى الخلفية‬
onPause () - onStop () :‫ودورة الحياة الخطوة‬

‫ مما يشير إلى أن النشاط ال يزال في‬،onDestroy () ‫• الحظ أنه لم يتم استدعاء حدث‬
.‫الذاكرة‬
‫ النشاط مرئي اآلن مرة‬."‫قم بإنهاء برنامج االتصال عبر الهاتف بالنقر فوق الزر "رجوع‬
، ‫أخرى‬
onRestart () - onStart () - onResume () :‫دورة الحياة الخطوة‬

You might also like