CSDN Blog: Http://blog.csdn.net/niu_gao
I think the Android system has a very disgusting person's big failure. It was this big failure that caused the card of the Android system to stop.
This big failure is the design of the activity.
Android's design principle for activity is that it must be independent at execution time.
You cannot create the activity directly and start it, and the system must help you do so. You're going to start the activity. Must use a intent request system to help.
So you have to declare every activity in the app in the manifest file. Assuming that you have multiple activity declarations that are bootable and specify Intent-filter, every activity can be a portal to your app.
Since each activity is likely to be a portal, each activity must be functionally independent. Self-fulfilling. In other words, the concept of process is watered down, and an activity hypothesis destroy. Even if the app process is not exiting and the new activity is started. should also be considered as the activity app to quit.
CSDN Blog: Http://blog.csdn.net/niu_gao
However the ideal is too plump, the reality does not bird you. How many people can have such a Communist mind and energy to put their every activity in the imaging system of the phone texting such an app? Most of the activity is used internally. Each one is very independent and laborious. In particular, it is more troublesome to share data between activity within the app, and the data should be placed in intent's additional data in accordance with its principles, starting the next activity with Startactivityforresult (). and receiving the data returned by the next activity in Onactivityresult (), but how much easier is it to assume that it's a global static data?
Isn't that too much of a design? You can kill activity or processes unrelated to the foreground app, but how can you kill activity in the foreground app? Rather than letting the system murder it, let the program ape kill it himself. Besides, assuming that the system memory is not keeping up with the normal execution of an app, is the app too poorly designed?
Another more chaotic situation: The latest Android system advocates navigation bar (do you want to learn from Apple, save a key?). ), the return button is available in the navigation bar. However, when you start B from activity A. Then on the nav bar of B return button return a when (in the manifest file will designate a as B's father), actually a is not the original a, but once again created one. Is this too much of a jerk? In such cases the Onactivityresult () in a is never executed! So note that the return button on the point navigation bar and the return key of the point device. The effect is different and different. A different.
。 Say three times about the thing that tore the egg!
And also. It is understood in the development documentation of Android that assuming that the activity has a background thread open, it should be closed in OnDestroy.
Well, generally an app is a multi-page navigation complete operation process, that is, open multi-threaded activity in the hidden and display between the switch to switch on and off switch multi-threaded?
Is there any way to solve the problem? Yes! Be able to take advantage of another function independently. Self-fulfilling components: service. The use of service to do background transactions, activity in the explicit will not affect the background tasks, but also do activity and service communication between, good trouble.
But there is better way to solve than not to be strong!
Not yet, because this design principle brings about a more chaotic activity lifecycle management problem! Take a look at this diagram, how scary the activity's life cycle is.
And here's the picture:
(about the activity's life cycle.) See: http://blog.csdn.net/niu_gao/article/details/7101178)
CSDN Blog: Http://blog.csdn.net/niu_gao
Complex even, the more fucked up is the inconsistency! Originally we thought onstop () must be corresponding onstart (), who knows halfway to kill a Onrestart (). OnStart () is not the implementation of the State, but also to Onresume () to calculate. OnCreate () is not actually the corresponding OnDestroy (), but the corresponding onstop (), I x. Can you curse?
But the most ridiculous thing is, if you want to save the state of the controls on the activity, you need to do it in OnPause ()! Crossing said, not OnPause (), but Onsaveinstancestate ().
Yes. It is actually onsaveinstancestate (), but the onsaveinstancestate () call timing is immediately followed by OnPause (). So there is not much difference from timing to OnPause (). That's why the Android interface card is stuck! The bones of the belt, can not be solved! Simply cause the OnPause () call to be followed by the hard drive (memory) operation! That is, the small activity switch, will cause the hard disk operation!
But actually in the process of executing an app. 99 of such operations are not required. Because the activity is not destroy! with Ben. All of the controls keep their state in memory! Why is the iOS interface smooth? Because there's no such fucking design in iOS. The Viewcontroller in iOS is very similar to activity. But there is no requirement to maintain independence during execution.
Android now has a place to copy iOS than a ListView alternative: Recycleveiw, whose item's weight and mechanism are very similar to the iOS UITableView cell, why not just learn about the design of the activity to iOS?
Another design of the ripped egg: when the screen rotates. Activity will be destroy re-create. According to the foregoing. To know the cost of destroy and the create process. This process of app process with Ben did not quit, for Mao I want to turn off the thread in the screen rotation process and then open? I'm going to turn off the network and re-connect? And what about iOS? Only a callback method is used to give the notification. Never turn off the view controller and rebuild. Of course, because rebuilding activity,android has the ability to be more bull B than iOS: horizontal and vertical screens can have a completely different layout. In fact, iOS is also able to, just want you to implement a callback method to respond to screen rotation, in which you can adjust the layout with code.
I think Android has been aware of this error, so the introduction of fragment this thing. Fragment Although there is a life cycle, but it will not be actively killed by the system, just want its activity is not dead. It's always there, and in fact you can make it leave the activity still alive. And you can decide fragment's life and death, assuming you want to save memory. You can take the initiative to kill fragment.
With fragment, your app can only have one activity, build multiple fragment, and the page switch is fragment switch between. This is in addition to the screen rotation. In most cases, there is no problem of destruction and reconstruction. Don't you believe me? You can see the API for managing fragment, there are ways to add fragment to the back stack. The back stack and the back button on the navigation bar don't exactly match up with an efficient wizard-type interface? So we should use more fragment less activity. Android launched fragment, just want you to do so, just embarrassed to put their original design global negation, hit their own face it.
(To learn more about fragment, see: http://blog.csdn.net/niu_gao/article/details/7163263)
All of these are personal prejudices, others are heavens beyond heavens outside. Want to have a master to hit my face.
CSDN Blog: Http://blog.csdn.net/niu_gao
Finally, let's talk about optimizations for Android programs:
android:configChanges=“orientation|keyboardHidden”
然后在activity中响应回调方法:
publicvoidonConfigurationChanged
CSDN Blog: Http://blog.csdn.net/niu_gao
Hopefully, in the new version number, Andorid will regain the concept of the process. Let everyone be free to decide whether to make the activity independent, further to iOS learning, the activity of pause to "into the background", the resume to "become the foreground", in addition to the life cycle of the most basic create and destroy outside, The rest of the renaming, the removal of the removed. So it's good for us, anyway, it's great.
Malicious Spit on Android (don't Look, poison)