Android:Activity组件学习笔记

一、Activity简介

1、Activity是android四大组件之一,在app中提供一个界面供用户交互,同时又具有生命周期,其生命周期交给系统去管理,我们在操作中只是给出指示而已。

2、同一个app中的activity相对其他activity是独立的,多个应用程序间的activity可以相互访问。例如,android官网上举的例子,摄像机可以调用邮箱来分享图片。

二、Activity栈

activity栈 由系统提供,用来管理应用程序中的activity。

三、Activity生命周期



1、activity的四种状态:

(1)runing状态:当activity处于当前屏幕最前端,即在activity栈顶,则处于running状态

(2)paused状态:当activity失去焦点,但是仍然可见,可能被一个透明地activity或者是以对话框形式出现的activity挡住,则处于paused状态。当activity处于该状态,它仍然保留了activity的信息,但可能因内存过低而被系统杀掉。

(3)stoped状态:当activity被另一个activity完全的遮挡住,则处在stoped状态。当activity处在该状态,它仍然保留了activity的状态信息,但已经不再可见了,很容易被系 统杀掉

(4)killed状态,finish状态:activity处于该状态,若想再次显示,只能重新onCreate()

2、activity一些常见状态

(1)activity1跳转到activity2,先调用activity1的onPause(),等activity2依次调用onCreate()、onStart()、onResume()之后,activity1再调用onStop() 。

(2)当用户点击手机上的Back回退键,当前的activity会依次调用onPause()、onStop()、onDestroy() ,所以点击back键后,默认activity被销毁 。

(3)当用户点击手机上的Home键或者电源键,当前的activity会依次调用onPause()、onStop() 。重新回到activity则会调用onRestart()、onStart()、onResume() 。

四、Activity传值

1、几种传值方式的代码

Intent intent = new Intent(MainActivity.this, SecondActivity.class);

//Bundle传值
Bundle bundle = new Bundle() ;
bundle.putString("name", "cigar");
bundle.putInt("age", 24);
intent.putExtra("info" , bundle) ;

//直接传值
intent.putExtra("sex", "男") ;

//Serializable对象传值
Student student = new Student() ;
student.setId(20100802931223L);
student.setName("cigar");
intent.putExtra("student",student) ;

//Parcelable对象传值
Person person = new Person();
person.setName("cigar");
person.setSex("男");
person.setAge(24);
intent.putExtra("person", person);

MainActivity.this.startActivity(intent);
2、获取值的代码

Intent intent = this.getIntent();
//获取传过来的bundle值
Bundle bundle = intent.getBundleExtra("info");
String name = bundle.getString("name");
int age = bundle.getInt("age");

//直接获取值
String sex = intent.getStringExtra("sex");

//获取Serializable对象
Student student = (Student) intent.getSerializableExtra("student");

//获取Parcelable对象
Person person = intent.getParcelableExtra("person");
3、实现Parcelable接口的源码

public class Person implements Parcelable {
    private String name;
    private String sex;
    private int age;

    public Person() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person(Parcel in) {
        name = in.readString();
        sex = in.readString();
        age = in.readInt();
    }

    public static final Parcelable.Creator<Person> CREATOR
            = new Parcelable.Creator<Person>() {
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

        public Person[] newArray(int size) {
            return new Person[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(getName());
        dest.writeString(getSex());
        dest.writeInt(getAge());
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

       实现Parcelable接口,必须实现public int describeContents() 和 writeToParcel(Parcel dest , int flags) 两个方法,其中describeContents默认不用动,在writeToParcel中将参数的值write进dest中。接着拷贝源码中的CREATOR,更改<MyParcelable>中的类型,这边改为Person,并定义一个带参数Parcel的构造方法 public Person(Parcel in) 。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章