Activities Flashcards
How are Android apps launched and how does this differ from other programming paradigms.
Unlike programming paradigms in which applications are launched with a main() method, the Android system initiates code in an Activity instance by
invoking specific callback methods that correspond to specific stages of its lifecycle.
Discuss activities as entry points for a use to an app and give an example
● Android application’s interaction does not always begin from an implied point the way traditional desktop and other apps do.
● Instead, an apps initial point is usual non-deterministic,
depending on the context f it’s launch, e.g, when launched
from a different app, the specific activity being called is what
launches
● When one app invokes another, the appropriate activity is usually launched, and not the app as an atomic whole.
● Therefore, an activity acts as an entry point for a use to the
app
How are activities implemented?
An activity is implemented as a subclass of the Activity class.
Define windows and screens in Android in relation to activities
An activity provides the window in which the app draws its UI.
● This window typically fills the screen, but may be smaller than the screen and float on top of other windows.
● Generally, one activity implements one screen in an app.
● An app has one main activity, which is the first screen that
appears when an app launches.
Define an activity in the manifest with all the required attributes
● To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element.</application></activity>
<manifest … >
<application … >
<activity></activity>
…
</application … >
…
</manifest >
Describe intent filters
Intent filters provide the ability to launch an activity based
not only on an explicit request, but also an implicit one.
Give examples of implicit and explicit requests
Sample explicit: “start a send message activity using WhatsApp”
● Implicit: “start a send message activity using an app able to do it on the phone”
Give an example of an intent filter in the manifest
<activity>
<intent-filter>
<action></action>
<category></category>
<data></data>
</intent-filter>
</activity>
page 7
Write code to call an activity with an intent
val sendIntent = Intent().apply {
action = Intent.ACTION_SEND
type = “text/plain”
putExtra(Intent.EXTRA_TEXT, textMessage)
}
startActivity(sendIntent)
Describe permissions and how they are handled across activities
● Permissions control the access different apps have for various
activities and content.
● To grant access to a given content or activity, the corresponding permissions need to be declared.
● In cases where we have parent and child activities, to enable
the parent class launch the child class, similar permissions need to be declared in both activities to ensure compliance.
Describe how permissions will be handled in a situation where an app wants to use another app
if an app wants to use a hypothetical app named
SocialApp to share a post on social media, SocialApp itself
must define the permission that an app calling it must have:
//Declaration in the SociaApp:
<manifest>
<activity android:name="...."
android:permission=”com.google.socialapp.permission.SHARE_POST”
/>
//Declaration in the app that calls the activity in SocialApp
<manifest>
<uses-permission></uses-permission>
</manifest>
</manifest>
List all the callbacks in the android life cycle
onCreate()
onStart()
onResume()
onPause()
onStop()
onRestart()
onDestroy()
Describe onCreate()
● A must callback in any activity
● Executes when system creates activity.
● Initializes essential activity components, eg, views and data binding
● setContentView() is called here to define activity’s UI layout
● Always precedes the onStart() activity
Describe onStart()
● Always succeeds onCreate()
● Makes activity visible to the user
Describe onResume()
● Invoked just before activity starts user interaction
● Usually done when the particular activity is at the top of the activity stack, thus captures data (user input)
● Implements most of the app’s core functionality
● Always precedes the onPause()