How to understand the lifecycle of an Activity Android Interview (3) : Is it really a good idea to update the UI with BroadcastReceiver? Do you really feel comfortable answering the phone? Android: Have you replaced commit() with apply() of SharedPrefrence?
Some small talk
It seems to have been a long time since the last article, and there doesn’t seem to be a lot of feedback, let alone reminders. Ha ha, give up.
The company is looking to hire Android developers with at least 5 years of experience. I’ve interviewed over 50 tech developers, but this is the first time I’ve interviewed Android, and it feels like a big girl getting into a sedan.
So I prepared very, very, very hard, and I started my role as Android interviewer with a lot of excitement.
Unfortunately, the feeling after the interview is that the development efficiency sounds very cool, and the third-party API is very easy to use. But when asked about the basis, I took the questions of my interview series to ask, but none of them can answer, even the good guidance, can not give a good answer.
The interview scene
Android development is familiar with data passing between two activities.
Well, sure. Intent.putxxx () is usually used to deliver lightweight data.
What about custom objects?
Just use putSerializable() of the Bundle. You can implement the Serializable interface with the Intent. PutExtras (Bundle).
Is there any other way to do this? What’s the difference?
I know there’s bundle.putParcelable (), but we mostly just use Serializable.
Why not use the Parcelable method? How are they different?
Because it’s simple, Serializable only requires one line of code to implement the interface, Parcelable I remember a lot of code. For the difference, em…… The forehead… HMM…
The body of the
The above scene, in fact, is in my recent occurrence. As a resume of 09 years into the old Android programmer, I am very sure of his ability to develop and solve problems, in this respect must beat me a lot of streets, but at least I asked the question let me a little surprised, asked the custom View drawing order, directly answer don’t know. Asked about LaunchMode, hemmed and hawed, not sure. In fact, it makes us think, what’s going on? Is it so important now that so many programmers write code that meets their needs? Fortunately, there are still many great gods who insist on the front line and strive to bring the foundation to everyone, such as Zhu Kai, the throwing line, and many other friends.
As most people probably know, the biggest difference between Serializable and Parcelable is the difference in efficiency. For small data, the difference is not that big, and the difference is not easy to see at the user level. But this does not mean that our development can ignore the gap of tens or even a few milliseconds.
Difference between Serializable and Parcelable
To be sure, both support serialization and deserialization operations.
The biggest difference between them lies in the different storage media. Serializable uses I/O read and write to store on hard disks, while Parcelable uses I/O read and write to store directly in memory. Obviously, memory reads and writes are usually faster than IO reads and writes, so Parcelable is preferred when passing data in Android.
Serializable will use reflection, serialization and deserialization process requires a lot of I/O operations, Parcelable own implementation of marshalling and unmarshalling (reflection) operation, data is also stored in Native memory, It’s much faster.
Someone directly compared the efficiency difference between the two
So we can see how do we write them?
- Serializable has always been synonymous with simplicity
public class TestSerializable implements Serializable {
String msg;
List<ItemBean> datas;
public static class ItemBean implements Serializable{ String name; }}Copy the code
- Parcelable speed is Paramount
public class TestParcelable implements Parcelable {
String msg;
@Override
public int describeContents(a) {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.msg);
}
TestParcelable(String msg) {
this.msg = msg;
}
private TestParcelable(Parcel in) {
this.msg = in.readString();
}
public static final Creator<TestParcelable> CREATOR = new Creator<TestParcelable>() {
@Override
public TestParcelable createFromParcel(Parcel source) {
return new TestParcelable(source);
}
@Override
public TestParcelable[] newArray(int size) {
return newTestParcelable[size]; }}; }Copy the code
Obviously, Parcelable isn’t easy to implement. It has tons of template code, which makes objects difficult to read and maintain. But if you really want to be a good Android developer, you probably need to spend more time on Parcelable. If you really want to be lazy, someone on GitHub has uploaded an Android Studio plugin to help you automatically generate these templates.
Address: github.com/mcharmas/an…
What else do you need to notice when passing objects between two activities?
Object size, object size, object size!!
Say important things three times. Be sure to pay attention to the size of the object. Bundles in intEnts use binders to deliver data. The size of the Binder buffer is limited (2 MB on some phones), and a process has 16 Binder threads by default, so the size of the buffer used by a single thread is much smaller (about 128 KB for a single thread was tested). So when you see The Binder transaction failed because it was too large such TransactionTooLargeException exceptions, you should know how to solve.