Android Activities and 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 called only once during the entire lifecycle of an activity when the application is initially launched?

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

An activity is no longer visible to the user. Which callback method is invoked, and what are the possible subsequent methods called?

  • `onStop()`, followed by `onRestart()` or `onDestroy()` (correct)
  • `onPause()`, followed by `onResume()` or `onStop()`
  • `onResume()`, followed by `onPause()` or `onDestroy()`
  • `onDestroy()`, followed by `onCreate()` or `onStart()`

In which scenario is the onDestroy() method called?

  • The activity is temporarily hidden by another activity.
  • The activity is finishing due to the user dismissing it or `finish()` being called. (correct)
  • The user navigates back to the activity from the background.
  • The device orientation changes, but the activity remains visible.

What is the purpose of an Intent in Android development?

<p>To request an action from another activity or app component. (D)</p> Signup and view all the answers

Which part of an Intent contains a reference to the data that the receiving activity should operate on?

<p>Intent Data/Object (A)</p> Signup and view all the answers

What is the key difference between an explicit intent and an implicit intent?

<p>Explicit intents specify the exact component to handle the intent, while implicit intents declare a general action to be performed. (C)</p> Signup and view all the answers

If you want to enable a left-facing arrow in the action bar that allows the user to navigate back to the parent activity, what attribute should you use in the <activity> element in the AndroidManifest.xml file?

<p><code>android:parentActivityName</code> (A)</p> Signup and view all the answers

What is the significance of the back stack in the context of Android activities?

<p>It is a stack that preserves the order of activities as the user navigates through them. (A)</p> Signup and view all the answers

When an activity transitions from the stopped state back to being visible and interactive, which callback method is invoked immediately before onStart()?

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

When using an explicit intent to start another activity, what does the this keyword represent in the Intent constructor (e.g., Intent intent = new Intent(this, Main2Activity.class);)?

<p>The current activity initiating the intent. (D)</p> Signup and view all the answers

Flashcards

Activity

A single, focused screen in an app for a specific task, presented full-screen to the user.

onCreate()

Method invoked when the app first launches; happens only once.

onStart()

Invoked just before the activity becomes visible to the user.

onResume()

Invoked just before the activity starts interacting.

Signup and view all the flashcards

onPause()

Invoked when the system is about to resume another activity.

Signup and view all the flashcards

onStop()

Invoked when no longer visible.

Signup and view all the flashcards

onDestroy()

Invoked when activity is finishing or being destroyed.

Signup and view all the flashcards

onRestart()

A preservation method for an activity after it comes back after being stopped. Always followed by onStart().

Signup and view all the flashcards

Intent

Asynchronous message to request an action from another activity/component.

Signup and view all the flashcards

Explicit intent

Target activity is already identified (class name known).

Signup and view all the flashcards

Study Notes

  • Activities represent a single screen in an app, allowing users to perform focused tasks, typically presented as a full-screen window
  • To implement an activity, create an Activity Java class, implement a basic UI in an associated XML layout file, and declare the new Activity in AndroidManifest.xml
  • File > New > Activity in Android Studio can automate these steps to start from a template

Activity Lifecycle

  • The activity life cycle is the set of states an activity can be in during its entire lifetime, beginning with creation and ending with destruction.

Callback Methods

  • onCreate() executes only once for the entire life of the activity, when the app is launched for the first time

  • onStart() is invoked before the activity becomes visible to the user, followed by either onResume() if the activity comes to the foreground or onStop() if the activity 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 either onResume() if the activity returns to the background, or onStop() if the activity becomes invisible

  • onStop() is invoked when the activity is no longer visible, followed by either onRestart(), which comes back to interact with the user or onDestroy() if the activity is about to end

  • onDestroy() is invoked when the activity is finishing due to being dismissed by the user, or due to finish() being called or destroyed because the system is temporarily due to a configuration change

  • onRestart() is invoked if the activity comes back after being stopped, and is always followed by onStart()

Intents

  • Activities are started or activated with an Intent, which is an asynchronous message, used to request an action from another activity or app component and to pass data
  • Parts of an intent include: Target activity, Intent data/object, Intent extras (optional), Intent flags (optional)

Types of Intents

  • Explicit Intent: The target of the intent is already identified by class name
  • Implicit Intent: The target is not yet identified, requiring a general action, category, and data type

Adding Intents

  • Within AndroidManifest.xml, activity elements can define properties like android:parentActivityName, indicating which activity is the parent enabling a left-facing arrow for navigation

  • To start another activity, add the android:onClick attribute to a Button element in XML, then define the corresponding method, which creates an Intent and starts the new activity

  • Adding a toast message in the launched activity verifies the intent works

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Mastering Android Activities
9 questions
Activities and Intents in Android Development
12 questions
Android Activities Overview
30 questions
Android Developer Fundamentals V2 - Activities
42 questions
Use Quizgecko on...
Browser
Browser