ACTIVITY, FRAGMENT, NAVIGATION Roberto Beraldi
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
Activity and anctivity manager GUI user input events Activity register with Activity Manager callback methods
States of an activity Running: the activity is in foreground and has the focus All events are delivered to it Paused: it is partially visible, but lost the focus For example the foreground activity doesn t occupy the whole screen (there is a dialog box in foreground) A paused activity maintains all state and member information, but the system can kill it Stopped: the activity is completely obscured by another one
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 History based navigation across activities is obtained through a LIFO back stack The activity in a task can belong to different applications For example picking a contact will lunch the contact application,
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
Back stack
Activity lifecycle Each activity in an application goes through its own lifecycle: it responds to a set of methods called by android Once and only once when an activity is created, is the oncreate( ) method executed If the activity exits, the ondestroy() method is executed In between, various methods are called allowing the activity to be harmonically managed
Lifecycle states In the Paused or Stopped state an activity can be killed by the am when resources are needed to the OS.
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()
Lifecycle methods Method: onstart() Called just before the activity becomes visible to the user or when the activity is becoming visible again Put code that can affect UI content Register a Broadcast receiver (see later)
Lifecycle methods 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. This is a good place to begin animations
Running state
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.
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.
Lifecycle methods Method: onrestart() Called after the activity has been stopped, just prior to it being started again. 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.
Example: change the device orientation oncreate onstart onresume onpause onsaveinstancestate onstop ondestroy oncreate onstart onrestoreinstancestate onresume
How an activity is created (explicitally)? An activity is created if it is the target of a special message called Intent An activity A starts another activity B by creating an explicit Intent with target B Uses a special method Context.startActivity(intent)
How an activity is created (implicitally)? An activity declares to the system its ability to perform actions through an intent-filter (declared in the manifest) The calling activity creates and intent with the action The activity manager presents a list of all activities that may perform the action (if more than one) Activity A start( ) Activity Action? Activity Manager
Launching an Activity Inside an application, an activity is marked as able to respond to action MAIN The launcher populates the home screen by retriving the activity can can respond the the MAIN action, category LAUNCHER When the icon is pushed, the Activity tagged as MAIN is started Indeed a new process is created from Zygote with shared libraries preloaded and specialized for the application (see:https://anatomyofandroid.com/)
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 I/INFO : B: onstop I/INFO : B: ondestroy note method calls are interleaved
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
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.
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
Saving the state
ActivityManager (demo)
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 create 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
Fragments and Activities Activity Fragment A CV Fragment CV B getactivity() Activity s lifecycle methods Fragment specific lifecycle methods Fragment Manager
Fragment lifecycle
Using Fragments
FragmentXML (demo) Two fragments One is ListFragment Activity is the glue among them Warning: onattach method changed in API 23!
Fragment via XML activity_main LinearLayout <fragment android:name=«.»> implementation Fragment Code Inflates firstfragement.xml (oncreateview) <fragment android:name=«.»> Fragment Code implementation Extends a ListFragment Activity Code implementation Inflates activity_main.xml
FragmentProgram (demo)
Fragments added programmatically Use a FragmentManager to add, remove fragments. After changes, use commit activity_main FrameLayout, id=1 placeholder (fragment container) FrameLayout, id=2
Screen navigation options Back navigation Hierarchical navigation Up navigation Descendant navigation Navigation drawer for descendant navigation Lists and carousels for descendant navigation Ancestral navigation Lateral navigation
Type of hierarchical navigation Descendant navigation Down from a parent screen to one of its children From a list of headlines to a story summary to a story Ancestral or up navigation Up from a child or sibling screen to its parent From a story summary back to the headlines Lateral navigation From one sibling to another sibling Swiping between tabbed views
Descendant navigation (with a list) News App Top Stories Tech News Cooking Story Story Story
Descendant navigation (fragments)
Descendant navigation (navigation drawer) 1.Icon in app bar 2.Header 3.Menu items
Up navigation
Lateral navigation News App Top Stories Tech News Cooking Story Story Story
A complete example
QUESTIONS?