activity and fragments introduction
play

ACTIVITY AND FRAGMENTS Introduction An application is composed of - PowerPoint PPT Presentation

ACTIVITY AND FRAGMENTS Introduction An application is composed of at least one Activity GUI It is a software component that stays behind a GUI (screen) Activity It runs inside the main thread and it should be reactive to


  1. ACTIVITY AND FRAGMENTS

  2. Introduction • An application is composed of at least one Activity GUI • It is a software component that stays behind a GUI (screen) Activity • It runs inside the ‘main’ thread and it should be reactive to user’s input • Managed by the ‘Activity Manager’

  3. How an activity is created? • An Activity can be created by the ‘Launcher’ • The corresponding icon on the home screnn is pushed • It is the starting point of an application • An activity can also be created by another activity or other sw components (see later) • An activity declares to the system its ability to perform ‘actions’ through an intent-filter (declared in the manifest) • The system will activate the activity when some other component in the system needs to perform the action

  4. How an Activity is created? callback methods GUl Activity Activity events Manager register with am start(…) Activity Activity Action? Activity Manager

  5. Sw hierarchy See: https://www.artima.com/insidejvm/ed2/threadsynchP.html java.lang.Object Interface to global information about an application environment. android.content.context Implemented by ‘android’. Lots of methods (see documentation) android.content.contextWrapper Delegate implementation android.view.contextThemeWrapper Allows to interact with theme android.app.Activity

  6. Activity permissions and features • An activity (or other SW components of an application) may require the permission to use some resources of the device • Permission are listed in the manifest file and granted at installation time • Starting with android 6.0, permission are also granted at execution time • An application may also require thre presence of same feature for being executed (e.g., a camera)

  7. Process, Task, Thread • Each application runs inside its own Process • Usually, other threads are created to peform long running operations • The set of activities lunched by a user is a Task • Simple navigation across activities is obtained through a LIFO back stack

  8. Back stack • Activities in the system are managed via an back stack • When a new activity is created, it is placed on the top of the stack and becomes active • The previous activity remains below in the stack until the running activity is deleted (for example back button) Running activity Ak A2 A1

  9. Back stack

  10. States of an activity • Running : the activity is in foreground and has the focus • The activity is visible and it has the focus, i.e., all events are delivered to it • Pause: it is partially visible, but lost the focus • For example the foreground activity doesn’t occupy the whole screen • A paused activity maintains all state and member information, but the system can kill it • Stopped: the activity is completely obscured by another one

  11. Life cycle states • In the Paused or Stopped state an activity can be killed by the am when resources are needed to the OS.

  12. Lifecycle methods • Each activity in an application goes through its own lifecycle. • Once and only once when an activity is created, is the onCreate() function executed. • If the activity exits, the onDestroy() function is executed. • In between, various events can lead to the activity being in multiple different states…

  13. ActivityLifecycle (demo) android:theme="@android:style/Theme.Dialog" I/INFO ﹕ A: onCreate I/INFO ﹕ A: onStart I/INFO ﹕ A: onResume (Button is clicked) I/INFO ﹕ A: onPause I/INFO ﹕ B: onCreate I/INFO ﹕ B: onStart I/INFO ﹕ B: onResume I/INFO ﹕ A: onSaveInstanceState (now the activity can be killed…) ( back botton is pressed ) I/INFO ﹕ B: onPause I/INFO ﹕ A: onResume note method calls are interleaved I/INFO ﹕ B: onStop I/INFO ﹕ B: onDestroy

  14. ActivityLifeCycle (demo) • I/INFO ﹕ A: onCreate • I/INFO ﹕ A: onStart • I/INFO ﹕ A: onResume ( second acivity is lunched an takes all the screen ) • I/INFO ﹕ A: onPause • I/INFO ﹕ B: onCreate • I/INFO ﹕ B: onStart • I/INFO ﹕ B: onResume • I/INFO ﹕ A: onSaveInstanceState • I/INFO ﹕ A: onStop ( First activity is no longer visible ) • I/INFO ﹕ B: onPause ( back button is now pressed ) • I/INFO ﹕ A: onRestart • I/INFO ﹕ A: onStart • I/INFO ﹕ A: onResume • I/INFO ﹕ B: onStop • I/INFO ﹕ B: onDestroy

  15. ActivityLifeCycle (demo) • Changing device’s orientation • …. • /INFO ﹕ A: onPause • I/INFO ﹕ A: onSaveInstanceState • I/INFO ﹕ A: onStop • I/INFO ﹕ A: onDestroy • I/INFO ﹕ A: onCreate • I/INFO ﹕ A: onStart • I/INFO ﹕ A: onRestoreInstanceState (this method is called when the activity is created again) • I/INFO ﹕ A: onResume

  16. Lifecycle methods • Method : onCreate(Bundle savedInstanceState) • Called when the activity is first created • This is where normal static initialization should go, e.g., create views, bind data to list, etc. • This method is passed a Bundle object containing the activity’s previous state ( null the first time). • Always followed by onStart()

  17. Lifecycle methods • Method: onStart() • Called just before the activity becomes visible to the user • It is called also when the activity is becoming visible again • Put additional initialization code • Method : onResume() • Called just before the activity starts interacting with the user. • At this point the activity is at the top of the activity stack, with user input going to it. • Here for example one can starts other threads…

  18. Lifecycle methods • Method: onRestart() • Called after the activity has been stopped, just prior to it being started again.

  19. Lifecycle methods • Method : onPause() • Called when the system is about to start resuming another activity. • This method is typically used to commit unsaved changes to persistent data, stop animations, threads, and any thing that may be consuming CPU, and so on. • It should do whatever it does very quickly, because the next activity will not be resumed until it returns. • Followed either by onResume () if the activity returns back to the front, or by onStop () if it becomes invisible to the user. • After this method, the activity is killable by the system.

  20. Lifecycle methods • Method : onStop() • Called when the activity is no longer visible to the user. • This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it. • Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.

  21. Lifecycle methods • Method : onDestroy() • Called before the activity is destroyed. • This is the final call that the activity will receive. • It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. • One can distinguish between these two scenarios with the isFinishing() method.

  22. Should you implement all methods? • All of these methods are hooks that one can override to do appropriate work when the state changes. • All activities must implement onCreate() to do the initial setup when the object is first instantiated. • It will also important ( recommended ) to implement onPause() to commit data changes and otherwise prepare to stop interacting with the user.

  23. Saving the state • Before to be killable, the onSaveInstanceState(Bundle savedInstanceState) is called • Here one can store specific activity’s state variables (in the bundle) • The Bundle is passed to onCreate method and to onRestoreInstanceState method

  24. ActivityManager (demo)

  25. Fragments • Useful for large screen (like tablet) • or as navigation tools • Fragments are hosted inside an Activity • A fragment is ‘attached’ to a View • It must creates its view (from an XML file) • Fragments have their own lifecycle (which is more complex than the activity) • Can be added via XML file or programmatically

  26. Fragments and Activities Activity Activity’s Fragment A Fragment B lifecycle methods CV CV Fragment specific lifecycle methods getActivity() Fragment Manager

  27. Fragment lifecycle

  28. Sw hierarchy java.lang.Object android.app.Fragment

  29. Using Fragments

  30. FragmentXML (demo) Two fragments One is ListFragment Activity is the glue among them Warning: onAttach method changed in API 23!

  31. FragmentProgram (demo)

  32. Wizard from android studio • Activity with one fragment • Builds the skeleton of an application with a list drawer and a main fragment. It also has an overflow button and action button

Recommend


More recommend