Documente Academic
Documente Profesional
Documente Cultură
Activity 2
Activity 3
Activity N
Android Manifest
LifeCycleTest
• Run the app and open the Logcat
• Every Android log message has a tag and a priority
associated with it.
• You define it in a Log method call, for example:
Log.d(tag, message);
• The priority is one of the following values:
• V — Verbose (lowest priority)
• D — Debug
• I — Info
• W — Warning
• E — Error
• A — Assert
Logcat
• Rotate emulator with CTRL+F11
Stopping – onStop()
• Many scenarios cause activity to be stopped
• Well behaved apps save progress and restart seamlessly
• Activity stopped when:
• user performs action in activity that starts another activity in the application
• user opens Recent Apps window and starts a new application
• user receives phone call
• use onStop to release all resources and save information
(persistence)
How to stop an Activity yourself?
•Generally, don't worry about it!
•"Note: In most cases, you should not explicitly finish an activity
using these methods. As discussed in the following section
about the activity lifecycle, the Android system manages the life
of an activity for you, so you do not need to finish your own
activities. Calling these methods could adversely affect the
expected user experience and should only be used when you
absolutely do not want the user to return to this instance of the
activity."
•methods: finish(), finishActivity()
Saving State
• Activities that are paused or stopped the state (instance vars) of the
activity are retained
• even if not in foreground
• When activity destroyed, the Activity object is destroyed
• can save information via onSaveInstanceState method. Write data to Bundle,
Bundle given back when restarted
Activity Destruction
• App may be destroyed under normal circumstances
• on its own by calling finish or user pressing the back button to navigate away
from app
• normal lifecycle methods handle this onPause() -> onStop() -> onDestroy
• If the system must destroy the activity (to recover resources or on an
orientation change) must be able to recreate Activity
Activity Destruction
Activity Destruction
• If Activity destroyed with potential to be recreate later, system calls
the onSaveInstanceState (Bundle outState) method
• Bundle is a data structure like a Map
• String keys
• put methods for primitives, arrays, Strings, Serializables (Java), and Parcels
(android)
UI Thread
• What is Thread?
• When application was running, as a default Android system will run a thread/
process
• One android application will be running on a thread, it’s called main
thread/ UI thread on android system
• What is the purpose of thread?
• There are two ways to execute code in a new thread.
• You can either subclass Thread and overriding its run() method, or
• construct a new Thread and pass a Runnable to the constructor.
• In either case, the start() method must be called to actually execute
the new Thread.
Fragment
•A Fragment represents a behavior or a portion of user
interface in an Activity.
•You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities.
•You can think of a fragment as a modular section of an
activity, which has its own lifecycle, receives its own input
events, and which you can add or remove while the activity is
running (sort of like a "sub activity" that you can reuse in
different activities).
Fragment (2)
• A fragment must always be embedded in an activity and the fragment's lifecycle
is directly affected by the host activity's lifecycle.
• For example, when the activity is paused, so are all fragments in it, and when the
activity is destroyed, so are all fragments.
• However, while an activity is running (it is in
• the resumed lifecycle state), you can manipulate each fragment independently,
such as add or remove them.
• When you perform such a fragment transaction, you can also add it to a back
stack that's managed by the activity— each back stack entry in the activity is a
record of the fragment transaction that occurred.
• The back stack allows the user to reverse a fragment transaction (navigate
backwards), by pressing
• the Back button.