Start of session
- Start Android Studio: it reopens Notes by itself. If the Welcome window shows instead: ProjectsNotes.
- Start the phone from the project: Device Manager (the phone icon on the window’s right edge, or ViewTool WindowsDevice Manager), ▶ on Pixel 7. It appears in Running Devices.
- Wait until indexing ends (the bar at the bottom) before Alt+Enter or Ctrl+O.
Before you start
By the end you can
- explain how
setContentView()turnsactivity_main.xmlinto objects; - write a line to Logcat and filter it;
- predict the methods Android calls for Home, coming back and a rotation.
- Java MainActivity.java
- Logcat
- Emulator
Behind, or a fresh project? Start from here about 4 min
The project: your own from Part 4, or a fresh one: Part 0, steps 0.3 and 0.4 (about 5 minutes).
Your package name, for the code of this page:
Without JavaScript, the code says com.yourname.notes: replace yourname by the name you chose.
Gradle: check these 3 lines. Add any that is missing, then click Sync Now. Keep your own version numbers.
Gradle Scripts › libs.versions.tomlAdd the marked lines[versions]agp = "9.5.0"junit = "4.13.2"junitVersion = "1.3.0"espressoCore = "3.7.0"appcompat = "1.8.0"material = "1.14.0"constraintlayout = "2.2.2"
[libraries]junit = { group = "junit", name = "junit", version.ref = "junit" }ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }material = { group = "com.google.android.material", name = "material", version.ref = "material" }constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
[plugins]android-application = { id = "com.android.application", version.ref = "agp" }Gradle Scripts › build.gradle.kts (Module :app)Add the marked linesplugins { alias(libs.plugins.android.application)}
android { namespace = "com.yourname.notes" compileSdk { version = release(37) }
defaultConfig { applicationId = "com.yourname.notes" minSdk = 24 targetSdk = 37 versionCode = 1 versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" }
buildTypes { release { optimization { enable = true packageScope = setOf("androidx.**", "kotlin.**", "kotlinx.**") } } } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 }}
dependencies { implementation(libs.appcompat) implementation(libs.material) implementation(libs.constraintlayout) testImplementation(libs.junit) androidTestImplementation(libs.espresso.core) androidTestImplementation(libs.ext.junit)}
lab1-part5-res.zip zip · 6.3 KBresand manifest: download the pack and open it in Explorer. Drag itsappfolder onto your project folder (C:\Users\<you>\), then choose Replace the files in the destination. Not Extract All: it makes a folder inside and replaces nothing. Then, in res › values › themes, delete themes.xml (night) if it is still there.AndroidStudioProjects\ Notes Java: open MainActivity.java, select all, then paste this file. Missing? First right-click kotlin+java › com.yourname.notes, the one without
(test), then NewJava Class: typeMainActivity, then press Enter.kotlin+java › com.yourname.notes › MainActivity.javaReplace the whole filepackage com.yourname.notes;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}▶. You are ready when the phone shows the styled notes screen: the gradient header, the composer, and 0 in the pill.
Step 1 of 5
Read your activity
Goal Explain in one sentence how activity_main.xml becomes your screen’s objects.
- Java kotlin+java › com.yourname.notes › MainActivity.java
-
Your activity, open in Android Studio, and its four notes.
- In the Project window, open kotlin+java › com.yourname.notes › MainActivity.java.
- Read the four notes under the code below: each number sits on its line.
package com.yourname.notes;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}extends AppCompatActivity: your class is an activity, a screen that Android can open.onCreate(): Android calls it when it creates the screen. Your app has nomain(): Android starts it (chapter 02).@Override: you redefine a method of the parent class.super.onCreate(…): the parent class does its own work first. It stays the first line.Ris a class the build writes, with oneintper resource.R.layout.activity_mainnames your layout file:setContentView()reads it and creates its objects.
- activity_main.xml your tags: a description of the screen
setContentView()reads the file thatR.layout.namesactivity_main - Objects one Java object per tag: a tree of views, in memory
- Pixels Android’s own toolkit draws the tree (chapter 01)
Step 2 of 5
Write your first line to Logcat
Goal Make your activity write “Main onCreate” to Logcat, then show only your lines.
- Java MainActivity.java
- Logcat package:mine tag:Lifecycle
-
Type A tag for your lines.
- In
MainActivity.java, click at the end of the class line,public class MainActivity …, then press Enter twice. - Type the panel’s line 10:
private static final String TAG = "Lifecycle";
kotlin+java › com.yourname.notes › MainActivity.javaAdd the marked linespackage com.yourname.notes;
import android.os.Bundle;import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lifecycle";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Main onCreate"); }} - In
-
Type Your first line for Logcat.
- Click at the end of the
setContentView(…)line, then press Enter. - Type the panel’s line 16:
Log.d(TAG, "Main onCreate"); - If
Logis red, click it, then press Alt+Enter. - A list opens: choose Import class. Android Studio writes line 4,
import android.util.Log;.
kotlin+java › com.yourname.notes › MainActivity.javaAdd the marked linespackage com.yourname.notes;
import android.os.Bundle;import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lifecycle";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Main onCreate"); }}Log.dwrites a debug line, levelD, labelled with your tag. Line 4, grey: Whole file shows it. - Click at the end of the
-
The app runs, and Logcat opens.
- Click ▶ Run 'app' in the toolbar (↻ Rerun if the app already runs).
- Open ViewTool WindowsLogcat: Logcat opens at the bottom of the window.
-
Only your lines, by their tag.
- Logcat’s query field already holds
package:mine: click right after it. - Type a space, then
tag:Lifecycle. One line stays:Main onCreate. - If a tip “Press Ctrl+Space to see suggestions” appears, click Got It.
Logcat Tool window · bottomFull size
- The query field:
package:mine tag:Lifecycle. - The process number (PID):
7837here; yours differs. - The tag:
Lifecycle. - Your message:
Main onCreate.
- Logcat’s query field already holds
Done I see my line “Main onCreate”, with its process number.
Stuck? 3 symptoms · the file
Cannot resolve symbol 'Log'The class is not imported. ClickLog, press Alt+Enter, then choose Import class.- Alt+Enter offers nothing Android Studio is still indexing: the bar at the bottom of the window. Wait for it to end.
- No line appears Check the query,
package:mine tag:Lifecycle, and the device menu at its left: Pixel 7 (emulator-5554) Android 17, API 37.0.
The files at the end of this step
package com.yourname.notes;
import android.os.Bundle;import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lifecycle";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Main onCreate"); }}Step 3 of 5
Add the six other methods
Goal Give your activity six more lifecycle methods, each writing its name to Logcat.
- Java MainActivity.java
-
The list of methods to override, opened inside the class.
- In
MainActivity.java, click right after the}that endsonCreate(), at the end of its line. - Press Ctrl+O. A Select Methods to Override/Implement window opens.
- In
-
onStart(), written by Android Studio.- Type
onstart: the selection jumps toonStart(): void, the only row selected. - Press Enter.
onStart()appears underonCreate(), itssuper.onStart();line selected in blue: type nothing yet.
Select Methods to Override/Implement A dialogFull size
- Type
onstart: the selection jumps to it. onStart(): the only method you pick.- OK, or Enter: writes the method.
- Type
-
Type Its line for Logcat.
- Click at the end of the
super.onStart();line, then press Enter. - Type line 22:
Log.d(TAG, "Main onStart");
kotlin+java › com.yourname.notes › MainActivity.javaAdd the marked linespackage com.yourname.notes;
import android.os.Bundle;import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lifecycle";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Main onCreate"); }
@Override protected void onStart() { super.onStart(); Log.d(TAG, "Main onStart"); }}Grey lines: Ctrl+O writes them.
- Click at the end of the
-
Paste
onResume()andonPause(), afteronStart().- On the panel below, find After line 23, then click Copy 12 lines.
- In
MainActivity.java, click at the end of line 23: the}that endsonStart(). - Press Enter, then Ctrl+V.
kotlin+java › com.yourname.notes › MainActivity.javaAdd the marked linespackage com.yourname.notes;
import android.os.Bundle;import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lifecycle";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Main onCreate"); }
@Override protected void onStart() { super.onStart(); Log.d(TAG, "Main onStart"); }
@Override protected void onResume() { super.onResume(); Log.d(TAG, "Main onResume"); }
@Override protected void onPause() { super.onPause(); Log.d(TAG, "Main onPause"); }} -
Paste
onStop()andonRestart(), afteronPause().- On the panel below, find After line 35, then click Copy 12 lines.
- In
MainActivity.java, click at the end of line 35: the}that endsonPause(). - Press Enter, then Ctrl+V.
kotlin+java › com.yourname.notes › MainActivity.javaAdd the marked linespackage com.yourname.notes;
import android.os.Bundle;import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lifecycle";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Main onCreate"); }
@Override protected void onStart() { super.onStart(); Log.d(TAG, "Main onStart"); }
@Override protected void onResume() { super.onResume(); Log.d(TAG, "Main onResume"); }
@Override protected void onPause() { super.onPause(); Log.d(TAG, "Main onPause"); }
@Override protected void onStop() { super.onStop(); Log.d(TAG, "Main onStop"); }
@Override protected void onRestart() { super.onRestart(); Log.d(TAG, "Main onRestart"); }
@Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "Main onDestroy"); }} -
Paste
onDestroy(), afteronRestart().- On the panel below, find After line 47, then click Copy 6 lines.
- In
MainActivity.java, click at the end of line 47: the}that endsonRestart(). - Press Enter, then Ctrl+V.
kotlin+java › com.yourname.notes › MainActivity.javaAdd the marked linespackage com.yourname.notes;
import android.os.Bundle;import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lifecycle";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Main onCreate"); }
@Override protected void onStart() { super.onStart(); Log.d(TAG, "Main onStart"); }
@Override protected void onResume() { super.onResume(); Log.d(TAG, "Main onResume"); }
@Override protected void onPause() { super.onPause(); Log.d(TAG, "Main onPause"); }
@Override protected void onStop() { super.onStop(); Log.d(TAG, "Main onStop"); }
@Override protected void onRestart() { super.onRestart(); Log.d(TAG, "Main onRestart"); }
@Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "Main onDestroy"); }}
Why Android calls only the parent’s exact names: Ctrl+O writes them, with @Override and super.
Stuck? 4 symptoms · the file
- Ctrl+O opens nothing The cursor is outside the class: click right after the
}ofonCreate(), and delete anyonstarttyped in the code. Or wait for indexing to end. - The
super.onStart();line vanished You typed while it was selected (blue). Ctrl+Z brings it back. - Your methods came in another order:
onResume()right afteronCreate()You picked several in the dialog: it writes them in its own order. They work the same. To match the page’s order, replace your whole file with the one below. compact source file should not have package declarationThe methods went after the class’s last}. Replace your whole file with the one below.

onResume(), right afteronCreate(): the dialog’s order, not the lifecycle order (onStart()comes first).onPause():@Overrideandsuper.onPause();written for you.
The files at the end of this step
package com.yourname.notes;
import android.os.Bundle;import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lifecycle";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "Main onCreate"); }
@Override protected void onStart() { super.onStart(); Log.d(TAG, "Main onStart"); }
@Override protected void onResume() { super.onResume(); Log.d(TAG, "Main onResume"); }
@Override protected void onPause() { super.onPause(); Log.d(TAG, "Main onPause"); }
@Override protected void onStop() { super.onStop(); Log.d(TAG, "Main onStop"); }
@Override protected void onRestart() { super.onRestart(); Log.d(TAG, "Main onRestart"); }
@Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "Main onDestroy"); }}Step 4 of 5
Predict, then check in Logcat
Goal Predict what Android calls when you leave, come back and turn the phone, then check in Logcat.
- Emulator Running Devices
- Logcat package:mine tag:Lifecycle
onCreate()the object is createdonStart()the screen becomes visibleonResume()in front: your taps reach it
onPause()no longer in frontonStop()no longer visibleonDestroy()the object is destroyed
Predict first
Write down what each action will show. First: after Home, which line comes last?
See the answer
Main onStop: Home stops your activity, without destroying it. The rest of the table waits after the actions.
-
Launch, leave, come back, turn: one table row at a time.
- In Logcat, click the trash icon at the top of its left column: Clear Logcat.
- Click the row’s button (Rerun in the main toolbar, the others in Running Devices), then do what Then says.
- Read your lines now: compare them with your prediction.
The four actions, in order Button Then 1 ↻ Rerun, or ▶ Run 'app' if the app is not running Nothing. 2 Home Nothing. 3 Overview The recent apps appear as cards: tap the one that shows your notes screen. 4 Rotate Left Nothing. In landscape the composer runs off the screen: Lab 1 keeps the phone upright. -
Upright again.
- In the Running Devices toolbar, click Rotate Right: the phone stands upright again.
-
Your predictions, checked.
- Open The answer table, just below: check each row against the lines you read.
The answer table
From a real run. onRestart() comes before onStart() when a stopped screen comes back.
| Action | Lines in Logcat |
|---|---|
| ▶ Run 'app' | Main onCreate · Main onStart · Main onResume |
| Home | Main onPause · Main onStop |
| Overview, then your app | Main onRestart · Main onStart · Main onResume |
| Rotate Left | Main onPause · Main onStop · Main onDestroy · Main onCreate · Main onStart · Main onResume |
Done I see my lines match the table.
Stuck? 2 symptoms
- Old lines mix with the new ones Clear Logcat before each action, with its trash icon.
- The screen does not turn Auto-rotate is off. Drag down twice from the top of the phone’s screen, then tap the rotation tile (two arrows around a phone) so it turns blue.
No file changes in this step: step 5.3 holds the file.
Step 5 of 5
Read what your lines prove
Goal Say what Home and a rotation do to your activity, with your lines as proof.
- Logcat package:mine tag:Lifecycle
This proves Home stops your activity without destroying it: leaving is not closing (chapter 03).
You should see10-31 19:36:49.584 5947 5947 D Lifecycle: Main onPause
10-31 19:36:50.722 5947 5947 D Lifecycle: Main onStop
This shows A rotation destroys your activity and creates a new object, in the same process: same process number.
You should see10-31 19:37:27.802 5947 5947 D Lifecycle: Main onPause
10-31 19:37:27.805 5947 5947 D Lifecycle: Main onStop
10-31 19:37:27.806 5947 5947 D Lifecycle: Main onDestroy
10-31 19:37:27.844 5947 5947 D Lifecycle: Main onCreate
10-31 19:37:27.844 5947 5947 D Lifecycle: Main onStart
10-31 19:37:27.850 5947 5947 D Lifecycle: Main onResume
Your times and process numbers will differ. These lines come from adb logcat: the Logcat window shows the same fields in columns.
Go deeper Back, and edge-to-edge 1 min
The Back button, on this first screen. Right after a start with ▶, Back calls onDestroy(). After a start from the app’s icon, or once you came back through Overview, it does not: Android keeps the activity, as for Home. Part 8 shows onDestroy() on Back, on its second screen.
Edge-to-edge. The Empty Views Activity template writes Android’s documented way: WindowCompat. and an insets listener in onCreate(). This lab needs neither: android:fitsSystemWindows, a standard attribute, does the job.
Now you
Count the new objects
Clear Logcat, then ▶. Click Rotate Left, Rotate Right, Rotate Left: how many Main onCreate lines?
See the answer
Four: one at launch, then one per rotation. Each rotation creates a new object of your activity. Three Rotate Left in a row give only two: the phone never turns upside down, and from one landscape to the other it keeps the activity.
If it doesn’t work
- “Waiting for all target devices to come online” The phone is still booting: wait.
error: package R does not existThe first line ofMainActivityis not your package. Copy the file again: the Copy the file button writes your package into it.- The phone opened in its own window, not in Running Devices You started it from the Welcome window. Close that window, then start it from the project’s Device Manager.
End of the part
Recap
No: Home only stopped your activity. A rotation destroyed it and created a new one. And setContentView() turns your XML into objects.
- explain how
setContentView()turnsactivity_main.xmlinto objects; - write a line to Logcat and filter it;
- predict the methods Android calls for Home, coming back and a rotation.
