Android development skills (a) - Get Context and Intent to transfer data

A global acquisitionContext

Android provides an Application class, whenever the program will automatically start the class initialization.
Application customize its own class, to facilitate management procedures within some global status information, such as global Context.

MyApplication.java

public class MyApplication extends Application {
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
    }

    public static Context getContext(){
        return context;
    }
}

Then the program starts to tell the system initialization MyApplicationclass, instead of the default Applicationclass. Modify AndroidManifest.xml:

    <application
        android:name="com.example.day22_skills.MyApplication"

But here to add the full package name, or can not find this class
so it implements a global acquisition Contextmechanism, then no matter you want to use anywhere Context, just call it MyApplication.getContext()on the line

Added: In order to make LitePalcompatible, but also need their own ApplicationuseLitePal.initalize(context)

Second, the use of Intentpassing objects

IntentGenerally used putExtra()transfer value, this can only be transmitted a number of commonly used data types, when want to pass some custom objects knowing where to start

1 Serializableway

Converting a sequence of objects is a state may be stored or transmitted, only need to implement serialization Serializableinterface can

For example there is a custom class People, in order to serialize him, achieving Serializableat the same time setting all fields getterand setterto

public class People implements Serializable {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

send data:

Person person = new Person();
person.setName("Tom");
person.setAge(20);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intetn.putExtra("person_data", person);
startActivity(intent);

Obtaining data need to call getSerializableExtra()the target sequence change downcast:

Person person = (Person) getIntent().getSerializableExta("person_data");

2, Parcelableway

This way than the Serialzablemore efficient, but some trouble, you need a complete decomposition of objects, each part of the decomposition is Intentsupported types:

public class People implements Parcelable {
    private String name;
    private int age;

......

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }

    public static final Parcelable.Creator<People> CREATOR = new Parcelable.Creator<People>(){
        @Override
        public People createFromParcel(Parcel source) {
            People people = new People();
            people.name = source.readString();
            people.age = source.readInt();
            return people;
        }

        @Override
        public People[] newArray(int size) {
            return new People[size];
        }
    };
}

First, implement Parcelablethe interface must describeContents()and writeToParcel()where describeContentsdirect returns 0, and in writeToParcel()the need to call Parcel.writeXXX()a method, in which the class of the field eleven write

People then provided in the class named CREATORconstants, there is provided an Parcelable.Creatorinterface to an implementation of the generic and designated People, then continue rewriting createFromParcel()and newArray():

  • createFromParcel(): Just read the written field, and create a People object and returns, order here must read and write the same order of just
  • newArray(): New People an array, and method of use passed sizeas an array size can be

Data obtained in this manner:

People people - (People) getIntent().getParcelableExtra("person_data");

ParcelableMode has a higher operating efficiency, so more is recommended

Published 180 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41205771/article/details/104443013