Android应用组件Activity

一、Activity简介

1、Activity拥有可以编辑用户界面的窗口,窗口布满整个屏幕,也可以小于或者漂浮于其他窗口之上

2、应用程序通常拥有多个互相关联的Activity和一个MainActivity


多个Activity之间跳转简单示意

Android应用组件Activity_第1张图片


二、Acitivity生命周期

Activity的声明周期是指Activity所经历的所有状态


Activty的 完整生命周期是指 从第一次调用onCreate()方法开始,直到调用onDrestroy()方法位置。
Activity的 可视生命周期是指  自onStart()调用开始直到相应的onStop()调用结束。在此期间,用户可以在屏幕上看到Activity。
Activity的前台生命周期是指 onResume()调用起,至相应的onPause()调用为止。在此期间,Activity位于前台最上面并与用户进行交互。


三、Activity的状态保存

通过onSaveInstance()方法,回收当前Activty时回调该方法
例:
package com.songjialongs.myapplication;

import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

public class Activity_Lifecycle extends AppCompatActivity {
    EditText user_name_et;
    CheckBox rem_user_name_cb;
    boolean iscckeck = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__lifecycle);
        user_name_et = (EditText) findViewById(R.id.user_name_et);
        rem_user_name_cb = (CheckBox) findViewById(R.id.rem_user_name_cb);
        iscckeck = savedInstanceState.getBoolean("remname");
        rem_user_name_cb.setChecked(iscckeck);
        final String name = savedInstanceState.getString("name");
        rem_user_name_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    user_name_et.setText(name);
                    iscckeck = true;
                } else {
                    iscckeck = false;
                }
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        String content = user_name_et.getText().toString();
        outState.putString("name", content);//参数为String Key,String Value
        outState.putBoolean("remname", iscckeck);//参数为String Key,boolean Value
    }
}
layout代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/user_name_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="用户名:"
        android:textSize="32sp" />

    <EditText
        android:id="@+id/user_name_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:layout_toRightOf="@id/user_name_txt" />

    <CheckBox
        android:id="@+id/rem_user_name_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/user_name_et"
        android:layout_below="@id/user_name_et"
        android:text="记住密码"
        android:textSize="24sp" />
</RelativeLayout>


四、Activity启动模式LaunchMode

LaunchMode是在Activity跳转的过程中,决定是否生成新的Activity,是否重用已经存在的,是否和其他实例公用一个task stack
1、Standard模式
Standard模式是默认启动模式,每次跳转Activity即StartActivty时都会生成一个新的Activity并压入系统默认的task stack中
Android应用组件Activity_第2张图片

Android应用组件Activity_第3张图片
package com.songjialongs.myapplication.LuancherMode;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.songjialongs.myapplication.R;

public class MainActivity extends AppCompatActivity {
    TextView activty_A_Txt;
    Button activty_A_Btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__main);
        activty_A_Txt = (TextView) findViewById(R.id.activty_a_txt);
        activty_A_Btn = (Button) findViewById(R.id.activity_a_btn);
        activty_A_Txt.setText(this.toString());
        activty_A_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Activity_Stardard_B.class);
                startActivity(intent);
            }
        });
    }
}


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.songjialongs.myapplication.R;

public class Activity_Stardard_B extends AppCompatActivity {
    TextView activty_B_Txt;
    Button activty_B_Btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__stardard);
        activty_B_Txt = (TextView) findViewById(R.id.stardard_b_txt);
        activty_B_Btn = (Button) findViewById(R.id.stardard_b_btn);
        activty_B_Txt.setText(this.toString());
        activty_B_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Activity_Stardard_B.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}



2、SingleTop模式
要跳转的Activity已经 存在与系统的task中,并且位于 栈顶,就使用栈顶的相同的Activity;若不存在,则创建一个新的Activty,并压入系统默认的task stack
Android应用组件Activity_第4张图片Android应用组件Activity_第5张图片
Android应用组件Activity_第6张图片
package com.songjialongs.myapplication.LuancherMode;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.songjialongs.myapplication.R;

public class MainActivity extends AppCompatActivity {
    TextView activty_A_Txt;
    Button activty_A_Btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__main);
        activty_A_Txt = (TextView) findViewById(R.id.activty_a_txt);
        activty_A_Btn = (Button) findViewById(R.id.activity_a_btn);
        activty_A_Txt.setText(this.toString());
        activty_A_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Activity_SingleTop_B.class);
                startActivity(intent);
            }
        });
    }
}



package com.songjialongs.myapplication.LuancherMode;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.songjialongs.myapplication.R;

public class Activity_SingleTop_B extends AppCompatActivity {
    TextView singletop_B_Btn_Txt;
    Button singletop_B_Btn_btn;
    Button singletop_BtoA_Btn_Txt;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__singletop__b);
        singletop_B_Btn_Txt = (TextView) findViewById(R.id.singletop_b_btn_txt);
        singletop_B_Btn_btn = (Button) findViewById(R.id.singletop_b_btn);
        singletop_BtoA_Btn_Txt = (Button) findViewById(R.id.singletop_btoa_btn);
        singletop_B_Btn_Txt.setText(this.toString());
        singletop_B_Btn_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Activity_SingleTop_B.this, Activity_SingleTop_B.class);
                startActivity(intent);
            }
        });
        singletop_BtoA_Btn_Txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Activity_SingleTop_B.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}


3、SingleTask模式
要跳转的Activity已经存在与栈中,就直接跳转至这个Activity;若不存在就创建一个新的Activity,并压入系统默认的task stack
Android应用组件Activity_第7张图片Android应用组件Activity_第8张图片
Android应用组件Activity_第9张图片
package com.songjialongs.myapplication.LuancherMode;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.songjialongs.myapplication.R;

public class MainActivity extends AppCompatActivity {
    TextView activty_A_Txt;
    Button activty_A_Btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__main);
        activty_A_Txt = (TextView) findViewById(R.id.activty_a_txt);
        activty_A_Btn = (Button) findViewById(R.id.activity_a_btn);
        activty_A_Txt.setText(this.toString());
        activty_A_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Activity_SingleTask_B.class);
                startActivity(intent);
            }
        });
    }
}



package com.songjialongs.myapplication.LuancherMode;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.songjialongs.myapplication.R;

public class Activity_SingleTask_B extends AppCompatActivity {
    TextView activty_B_Txt;
    Button activty_B_Btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__singletask);
        activty_B_Txt = (TextView) findViewById(R.id.singletask_b_txt);
        activty_B_Btn = (Button) findViewById(R.id.singletask_b_btn);
        activty_B_Txt.setText(this.toString());
        activty_B_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Activity_SingleTask_B.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}


4、SingleInstance模式
跳转Acitivity 新建一个task stack,将跳转的Activity压入这个task stack,并且新建的task stack只有一个Activity
Android应用组件Activity_第10张图片Android应用组件Activity_第11张图片
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.songjialongs.myapplication.R;

public class MainActivity extends AppCompatActivity {
    TextView activty_A_Txt;
    Button activty_A_Btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activty_A_Txt = (TextView) findViewById(R.id.activty_a_txt);
        activty_A_Btn = (Button) findViewById(R.id.activity_a_btn);
        activty_A_Txt.setText(this.toString() + ":栈:" + getTaskId());
        activty_A_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Activity_SingleInstance_B.class);
                startActivity(intent);
            }
        });
    }
}

package com.songjialongs.myapplication.LuancherMode;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.songjialongs.myapplication.R;

public class Activity_SingleInstance_B extends AppCompatActivity {
    TextView singinstance_B_Txt;
    Button singinstance_B_Btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__singleinstance);
        singinstance_B_Txt = (TextView) findViewById(R.id.singleinstance_b_txt);
        singinstance_B_Btn = (Button) findViewById(R.id.singleinstance_b_btn);
        singinstance_B_Txt.setText(this.toString() + ":栈:" + getTaskId());
        singinstance_B_Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Activity_SingleInstance_B.this, Activity_SingleInstance_B.class);
                startActivity(intent);
            }
        });
    }
}

你可能感兴趣的:(Android应用组件Activity)