Android:细谈Activity四种启动模式
Posted JMW1407
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android:细谈Activity四种启动模式相关的知识,希望对你有一定的参考价值。
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ActivityTest">
<activity android:name=".NormalActivity" />
<activity android:name=".MainActivity"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/startNormalActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start NormalActivity" />
<Button
android:id="@+id/startDialogActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start DialogActivity" />
</LinearLayout>
activity_normal.xml
<?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"
tools:context=".NormalActivity"
android:orientation="vertical">
<Button
android:id="@+id/startMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start MainActivity" />
<Button
android:id="@+id/startDialogActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start DialogActivity" />
</LinearLayout>
NormalActivity
package com.example.activitytest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class NormalActivity extends AppCompatActivity {
private final String Tag = "=====NormalActivity:";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_normal);
Log.d(Tag,this.toString());
Button button1 = this.findViewById(R.id.startMainActivity);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NormalActivity.this, MainActivity.class);
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NormalActivity.this, dialogActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("NormalActivity","==onDestroy()被调用==");
}
}
MainActivity
package com.example.activitytest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private final String Tag = "====MainActivity:";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Tag,this.toString());
setContentView(R.layout.activity_main);
//Log.d(Tag,"onCreate()被调用");
Button button1 = this.findViewById(R.id.startNormalActivity);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NormalActivity.class);
startActivity(intent);
}
});
// button2.setOnClickListener((onclick));
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("MainActivity","===onRestart()被调用===");
}
/*View.OnClickListener onclick = new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, dialogActivity.class);
//intent.putExtra("data", "mainActivity");
startActivity(intent);
}
};*/
/*@Override
protected void onStart() {
super.onStart();
Log.d("MainActivity","onStart()被调用");
}
@Override
protected void onResume() {
super.onResume();
Log.d("MainActivity","onResume()被调用");
}
@Override
protected void onPause() {
super.onPause();
Log.d("MainActivity","onPause()被调用");
}
@Override
protected void onStop() {
super.onStop();
Log.d("MainActivity","onStop()被调用");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("MainActivity","onDestroy()被调用");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("MainActivity","onRestart()被调用");
}*/
}
以上是关于Android:细谈Activity四种启动模式的主要内容,如果未能解决你的问题,请参考以下文章