Android App Activity Lifecycle

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which callback method is invoked only once during the entire lifecycle of an activity?

  • `onStart()`
  • `onResume()`
  • `onCreate()` (correct)
  • `onPause()`

An activity transitions from the visible state to a state where it is no longer visible. Which callback method is invoked during this transition?

  • `onStart()`
  • `onPause()` (correct)
  • `onRestart()`
  • `onResume()`

Under what circumstance is the onDestroy() callback method invoked?

  • When the activity is resumed after being stopped.
  • When the activity is finishing or being destroyed due to configuration change. (correct)
  • When the activity becomes hidden.
  • When the activity is initially created.

An activity is currently stopped. Which callback method is invoked if the activity returns to interact with the user?

<p><code>onRestart()</code> (B)</p> Signup and view all the answers

What role does the back stack play when a new activity starts?

<p>It stops the previous activity but preserves it in a stack. (A)</p> Signup and view all the answers

Which of the following accurately describes the purpose of an Intent in Android development?

<p>It's a message used to request an action from another activity or app component. (C)</p> Signup and view all the answers

An Intent needs to pass data to the receiving activity. Which part of the Intent is responsible for carrying this data?

<p>Intent data/object (C)</p> Signup and view all the answers

How might an Intent, through the use of flags, influence the Android system when launching an activity?

<p>By instructing the system on how to treat an activity post-launch. (C)</p> Signup and view all the answers

What differentiates an explicit Intent from an implicit Intent?

<p>Explicit <em>Intent</em> specifies the exact component to handle the <em>Intent</em>, while implicit <em>Intent</em> declares a general action. (B)</p> Signup and view all the answers

If you want to specify the exact component to handle the Intent, which type of Intent would you use?

<p>Explicit <em>Intent</em> (D)</p> Signup and view all the answers

How do you programmatically set up navigation between two activities using explicit Intents?

<p>By creating an <em>Intent</em> that specifies the target activity and starting it using <code>startActivity()</code>. (B)</p> Signup and view all the answers

What is the significance of the parentActivityName attribute in the <activity> element of the AndroidManifest.xml file?

<p>It indicates the activity that is the logical parent of the current activity, enabling navigation via a left-facing arrow. (B)</p> Signup and view all the answers

In the context of Android activities, what does the term "activity lifecycle" refer to?

<p>The different stages an activity goes through, from creation to destruction. (C)</p> Signup and view all the answers

Consider an activity that is currently visible and running in the foreground. If a phone call interrupts the user, which callback method is MOST likely to be invoked first?

<p><code>onPause()</code> (B)</p> Signup and view all the answers

What is the primary purpose of calling super.onCreate(savedInstanceState) within the onCreate() method of an Activity?

<p>To ensure that the parent class's <code>onCreate()</code> method is executed, which is important for proper setup and state restoration. (B)</p> Signup and view all the answers

In the code snippet android:onClick="launchNextActivity", what does launchNextActivity refer to?

<p>A method defined within the activity that handles the button click. (C)</p> Signup and view all the answers

If a user rotates their device, what is the typical behavior of an Android activity, and which lifecycle methods are involved?

<p>The activity is destroyed and recreated, involving <code>onDestroy()</code> and <code>onCreate()</code>. (C)</p> Signup and view all the answers

To display a user interface for an activity, which method is commonly used and what parameter does it take?

<p><code>setContentView()</code>, taking a layout resource ID as a parameter. (B)</p> Signup and view all the answers

Which combination of steps is required to properly implement a new Activity in an Android application?

<p>Create a Java class that extends Activity, declare the Activity in AndroidManifest.xml, and implement a basic UI in an associated XML layout file. (C)</p> Signup and view all the answers

What purpose does Toast.makeText(getApplicationContext(), "New activity launched!", Toast.LENGTH_SHORT).show(); serve in verifying Intent functionality?

<p>It displays a short message on the screen to confirm the action's success. (D)</p> Signup and view all the answers

Flashcards

Activity (in Android)

Represents a single screen in an app for a focused task, usually full-screen.

onCreate()

Called when the app is launched; happens only once per the activity's life.

onStart()

Called before the activity becomes visible to the user.

onResume()

It is invoked before the activity starts interacting with the user.

Signup and view all the flashcards

onPause()

Called when the system is about to start resuming another activity.

Signup and view all the flashcards

onStop()

Called when the activity is no longer visible to the user.

Signup and view all the flashcards

onDestroy()

Called when the activity is finishing or being destroyed by the system.

Signup and view all the flashcards

onRestart()

Invoked if the activity comes back after being stopped, always followed by onStart().

Signup and view all the flashcards

Back Stack

A stack where the system preserves the previous activity when a new activity starts.

Signup and view all the flashcards

Intent (in Android)

Asynchronous message used to request an action from another activity or app component.

Signup and view all the flashcards

Target Activity

The activity that will receive the intent.

Signup and view all the flashcards

Intent Data/Object

Contains a reference to the data the receiving activity will operate on.

Signup and view all the flashcards

Intent Extras

Carry information the receiving activity needs to accomplish the requested action.

Signup and view all the flashcards

Intent Flags

Instruct the Android system how to launch or treat the Activity after it's launched.

Signup and view all the flashcards

Explicit Intent

The target of the intent is already identified (the class name of the activity).

Signup and view all the flashcards

Implicit Intent

The target is not yet identified, but there is a general action, category, and data type.

Signup and view all the flashcards

parentActivityName

Indicates the main activity is the parent of the second activity.

Signup and view all the flashcards

Study Notes

  • Represents a single screen in an app where a user does tasks like sending email
  • Usually shown to the user as a full-screen window

Activity Implementation

  • First, create an Activity Java class
  • Next, implement a basic UI for the Activity in an associated XML layout file
  • Finally, declare the new Activity in AndroidManifest.xml
  • To automate these tasks in Android Studio, use File > New > Activity to start from a template
  • MainActivity.java is presented to the user when the app launches and can start other activities

Activity Life Cycle

  • The set of states an activity can be in during its entire lifetime
  • The callback methods used during the activity lifecycle include: onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy(), and onRestart()

Callback Methods

  • onCreate() is invoked when the app launches for the first time, only once for the entire lifespan
  • onStart() is invoked before the activity is visible, followed by onResume() if it comes to the foreground, or onStop() if it becomes hidden
  • onResume() is invoked before the activity starts interacting with the user
  • onPause() is invoked when the system is about to start resuming another activity, followed by onResume() if the activity returns to the background, or onStop() if it becomes invisible
  • onStop() is invoked when the activity is no longer visible, followed by onRestart() if the activity is coming back to interact with the user, or onDestroy() if the activity is about to end
  • onDestroy() is invoked when the activity is finishing because the user is completely dismissing it, or the system is temporarily destroying the activity due to a configuration change
  • onRestart() is invoked if the activity comes back after being stopped and is always followed by onStart()
  • Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in the back stack

Intents

  • An Intent is an asynchronous message used in an activity to request an action from another activity or app component
  • Intents can start activities and pass data

Intent Parts

  • Target activity - the activity receiving the intent
  • Intent data/object - reference to data for the receiving activity
  • Intent extras - carries information for the receiving activity (optional)
  • Intent flags - instructs the system how to launch or treat the activity (optional)

Intent Types

  • Explicit intent's target (activity class name) is already identified
  • Implicit intent's target is not yet identified, but there is a general action to perform, including action, category, and data type
  • The parentActivityName attribute indicates the main activity is the parent of the second activity and enables a left-facing arrow for navigation

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Android Main Activity Logic Quiz
18 questions
Android Activity Lifecycle
17 questions

Android Activity Lifecycle

AdaptiveMoldavite7334 avatar
AdaptiveMoldavite7334
Android Activity Lifecycle Quiz
21 questions
Android App Architecture Quiz
40 questions

Android App Architecture Quiz

ProtectiveBohrium3356 avatar
ProtectiveBohrium3356
Use Quizgecko on...
Browser
Browser