Activities Димитър Н. Димитров Astea Solutions AD
Recap: Android components Activity Service BroadcastReceiver ContentProvider Intent (?)
Starting an Android component – components can be started with Intents – Android creates the components and manages their lifecycle Application flow – in Java: everything starts from the main() method – in Android: no obligations for single entry point – pretty much everything you see in the application flow is managed by an Activity Applications and components
Activity “...a single, focused thing that the user can do.” creates a window and initializes the UI for it reacts on user interactions allows you to interact with the system functions and services – see android.content.Context
Activity lifecycle Activity states – resumed (running) – paused – stopped Lifecycle loops – entire lifetime – visible lifetime – foreground lifetime
Activity lifecycle Entire lifetime
Activity lifecycle Visible lifetime
Activity lifecycle Foreground lifetime
Activity lifecycle Activity is “killable” after: onPause() onStop() onDestroy() Two-Activity scenario: – A.onPause() – B.onCreate() – B.onStart() – B.onResume() – A.onStop() (already not visible)
android.app.Activity Starting an activity void startActivity(Intent intent)Intent void startActivityForResult(Intent intent, int requestCode)Intent – AndroidManifest.xml Important methods void setContentView() void onSaveInstanceState(Bundle)/void onRestoreInstanceState(Bundle)
Tasks Android groups Activities, started in order to perform a certain job in tasks – tasks are stacks (first in, first out) – clicking the hardware BACK button pops the current Activity from the stack, by default How many instances of a given Activity can I have? How can I start an Activity in a new task? What Activity will be resumed, when I press BACK?
Activities & your application All the different “screens” in your application should be different Activities – try not to put too much stuff in a single Activity Activities should encompass most of the business logic of your application – Activities act as a controller in the MVC pattern Start with a simple diagram and evolve it
Breaking news! Android 4.0 has just been released! It should bridge the gap between Android APIs for tablets and smartphones
Q&A + Feedback Questions? Feedback section: – Did you hear well? – Was there anything you didn’t understand? – What would you like changed in our next lecture?
Recommended resources als/activities.html als/activities.htm als/tasks-and-back-stack.html als/tasks-and-back-stack.html elines/activity_task_design.html elines/activity_task_design.html %E2%80%93controller %E2%80%93controller