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) 。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章