テスト プロジェクトを作成し、アクティビティのライフ サイクルを確認および観察し、インテントを使用してアクティビティ間を移動します。
1. TestActivity プロジェクトを作成します。
2. SecondActivity と対応するレイアウトを作成します。
3. MainActivity や SecondActivity の onStart() や onResume() などのライフサイクル イベント メソッドを書き換え、Log を使用してログ情報を出力します。
4. MainActivity でボタンをクリックする onClick() メソッドを記述し、Intent を使用して SecondActivity を開きます。
5. プログラムを実行し、アクティビティの作成、表示、終了、切り替え、その他の表示回復プロセス中に各ライフ サイクル イベント メソッドがトリガーされることを観察します。
0.プロジェクトの作成
要件に従ってプロジェクトを作成するだけです
1. SecondActivity と対応するレイアウトを作成する
Android Studio で、プロジェクトapp
ディレクトリを右クリックし、[新規] > [アクティビティ] > [空のアクティビティ] を選択します。
新しいアクティビティに という名前を付けSecondActivity
、レイアウト ファイルの作成時に選択しますactivity_second.xml
でactivity_second.xml
レイアウトを定義します
TextView と 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>
効果を達成する
2. ライフサイクルメソッドをオーバーライドする
TAGをインポートして定数タグを設定する
必ずandroid.util.Log
クラスをインポートし、定数タグを設定してください
private val TAG = "SecondActivity"
MainActivity
とではSecondActivity
、観察が必要なライフサイクル メソッド (例MainActivity
:など) をオーバーライドします。SecondActivity
onStart()
onResume()
Log
そして、出力されたログ情報をこれらのメソッド内で使用します。
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart() called");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
Android Studio に直接コピーすると、自動的に変換されます。!
変換されたコード
override fun onStart() { super.onStart() Log.d(TAG, "onStart() called") } override fun onResume() { super.onResume() Log.d(TAG, "onResume() called") }
MainActivity コード
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 コード
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.SecondActivityを開く
追加ボタン
ボタンを追加し、ボタンに属性をactivity_main.xml
追加します。これにより、ボタンのクリック イベントとメソッドが関連付けられます。android:onClick="openSecondActivity"
openSecondActivity
activity_main.xml
存在しない場合は自分で作成します
<?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>
メソッドの追加
MainActivity にボタンを記述する方法onClick()
:
public void openSecondActivity(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
ktlionコード
fun openSecondActivity(view: View?) { val intent = Intent(this, SecondActivity::class.java) startActivity(intent) }
4. プログラムを実行し、ライフサイクル イベントを観察します。
- Android Studio で、緑色の [実行] ボタンをクリックしてアプリを実行します。
- エミュレータまたは接続された Android デバイスで、ボタンをタップして へのジャンプをトリガーします
SecondActivity
。 - Logcat ウィンドウの出力を観察し、ライフサイクル メソッドの呼び出しを確認し、アクティビティの作成、表示、終了、切り替えのプロセスを理解します。
外部リンク画像の転送に失敗しました。ソース サイトにはリーチ防止メカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします。
表示される場合
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.
- 非互換性の問題があります
mainifests
をクリックして、テーマを次のように変更する必要があります。@style/Theme.AppCompat.DayNight.DarkActionBar
効果を達成する