Create a test project, verify and observe the Activity life cycle, and use Intent to jump between Activities:
1. Create the TestActivity project;
2. Create SecondActivity and corresponding layout;
3. Rewrite life cycle event methods such as onStart() and onResume() in MainActivity and SecondActivity, and use Log to output log information;
4. Write the onClick() method of clicking the button in MainActivity, and use Intent to open SecondActivity;
5. Run the program and observe the triggering of each life cycle event method during the creation, display, closing, switching and other display recovery processes of the Activity.
0.Create project
Just create a project according to your requirements
1. Create SecondActivity and corresponding layout
In Android Studio, right-click the project app
directory and select "New" > "Activity" > "Empty Activity".
Name the new Activity as SecondActivity
and choose when creating the layout fileactivity_second.xml
Define theactivity_second.xml
layout in
Here is a simple example containing a TextView and a Button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is SecondActivity"
android:textSize="18sp"
android:textColor="#000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back"
android:id="@+id/backButton"/>
</LinearLayout>
achieve effect
2. Override life cycle methods
Import TAG and set constant tags
Make sure to import android.util.Log
the class and set a constant tag
private val TAG = "SecondActivity"
In MainActivity
and SecondActivity
, override the lifecycle methods that require observation (e.g. MainActivity
, etc. ).SecondActivity
onStart()
onResume()
Log
And use the output log information inside these methods .
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart() called");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
Copy directly into Android Studio and it will be automatically converted! !
converted code
override fun onStart() { super.onStart() Log.d(TAG, "onStart() called") } override fun onResume() { super.onResume() Log.d(TAG, "onResume() called") }
MainActivity code
package com.leo.activity_1
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.ComponentActivity
class MainActivity : ComponentActivity() {
private val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
}
override fun onStart() {
super.onStart()
Log.d(TAG, "onStart() called")
}
override fun onResume() {
super.onResume()
Log.d(TAG, "onResume() called")
}
fun openSecondActivity(view: View?) {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
}
SecondActivity code
package com.leo.activity_1
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
class SecondActivity : AppCompatActivity() {
private val TAG = "SecondActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.second)
}
override fun onStart() {
super.onStart()
Log.d(TAG, "onStart() called")
}
override fun onResume() {
super.onResume()
Log.d(TAG, "onResume() called")
}
fun goBack(view: View?) {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
3.Open SecondActivity
Add button
Add a button in activity_main.xml
and add android:onClick="openSecondActivity"
attributes to the button, which will associate the button click event and openSecondActivity
method
If
activity_main.xml
it does not exist, create it yourself
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Second Activity"
android:id="@+id/openSecondButton"
android:layout_centerInParent="true"
android:onClick="openSecondActivity" />
</RelativeLayout>
Add method
How to write buttons in MainActivity onClick()
:
public void openSecondActivity(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
ktlion code
fun openSecondActivity(view: View?) { val intent = Intent(this, SecondActivity::class.java) startActivity(intent) }
4. Run the program and observe life cycle events:
- In Android Studio, click the green Run button to run your app.
- On the emulator or connected Android device, tap the button to trigger the jump to
SecondActivity
. - Observe the output in the Logcat window, view the calling of life cycle methods, and understand the creation, display, closing and switching process of Activity
The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.
If it appears
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.leo.activity_1/com.leo.activity_1.SecondActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- there is an incompatibility problemNeed to click
mainifests
, change the theme to@style/Theme.AppCompat.DayNight.DarkActionBar
achieve effect