Android:细谈Activity四种启动模式

Activity四种启动模式


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()被调用");
  }*/

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值