(i) Preface
In a previous post about AMS, mention a little bit of tabactivity. At that time said it is more special there is no following, today, re-send a post, to discuss with you about tabactivity.
Make a hypothesis: for example, our outermost activity is mainactivity, the first tab is Firstactivty, the Second tab is secondactivity.
I believe everyone has used tabactivity, it is a special activity, where is its special place? The following points are for proof:
A. It appears to violate the principle of a single window of activity. Because it can load several activity at the same time, when the user clicks on the tab above it, it jumps to the corresponding activity.
B. The user first goes in to Firstactivity, then goes in to secondactivity, and then clicks Back button. It returns an interface that is not firstactivity, but exits our application.
C. When the user presses the return key in Firstactivity, if Mainactivity and firstactivity by overriding the OnKeyDown () method, then only the firstactivity of the event callback is received.
Two The need for tabactivity and Google's obsession
A. First of all, we have to understand that Android is a single Window system, unlike Windows is multi-windows (such as on Windows systems, we can chat QQ, while fighting landlords and so on). That is, in a moment, only one activity in Android can be displayed to the user.
This greatly reduces the complexity of the operating system design (including event dispatch, etc.).
B. But the effect, like tabactivity, is very necessary and the user experience is better. So I think Google developers at that time must be very tangled. Thus, a deformed idea arose, that is, in a single Window system loading multiple activity, it is tabactivity.
Three Tabactivity implementation loading multiple activity principles
As we all know, to start an activity, it is generally called the startactivty (Intent i) method. This method is then called to AMS (Activitymanagerservice) to initiate the target activity.
So, there are two key points to achieve in tabactivity:
A. Find a portal that accesses the Activitythread class (the class is hidden and the application is inaccessible) and then invokes the start activity method inside the Activitythread
B. Bypassing AMs, that is, the firstactivity and secondactivity that we tabactivity load cannot be known to AMS.
So, a new class was born----Localactivitymanager, and its function is as follows:
1. This class and Activitythread are in a package, so it has access to activitythread.
2. This class provides a way to manage activity like AMS, such as calling activity's OnCreate method, Onresume () and so on, maintaining the activity life cycle, as compared to AMS, like the big Lei Yinji and small Lei Yinji in the journey to the monkey.
As well as its name, it is the local activity management. That is, it runs the process and the activity it manages is inside a process.
So, when tabactivity to start an activity, it calls the Create activity method to Localactivitymanager, and then calls Activitythread.startactivitynow (), This method bypasses AMs, which means that AMS does not know at this time that Localactivitymanager has started an activity in sneak (so there are no new activity records in AMS's Task list, So the user presses the back key to exit our app directly).
Then, as with normal startup activity, there is an important way to initialize the activity when initializing the activity: Activity.attch ()
final void attach (...) {
....
Mwindow.setcallback (this);
.....
}
Mwindow.setcallback (this); Very important, it sets the callback interface of the window, which is the key to our activity being able to receive the key event! Because the Decorview will callback this interface when it receives the event.
such as: final Callback cb = Getcallback ();
Final Boolean handled = CB! = NULL && Mfeatureid < 0? Cb.dispatchkeyevent (Event)
: Super.dispatchkeyevent (event);
When we start firstactivity, we set firstactivity to Phonewindow's callback implementation, so when you press the back key, the Firstactivity onkeydown method is called.
Four The status quo of tabactivity
The 2nd above says that Tabactivity is just a freak. Therefore, in the later development will certainly be replaced. Sure enough, Google in android3.0 launched the fragment this east, this east can replace Tabactivity.
Inevitably, the classes associated with Tabactivity are declared as deprecated, including its parent class Activitygroup, already our little Lei Yinji localactivitymanager ....
Write here, not only a kind of hero twilight, the feeling of the beauty of the twilight, suddenly think of the recent hit of the Chu-Han saga, Chu Overlord Martial a world, to the end, also unavoidably Ziwen in the Wujiang River. Tabactivity once in the android2.2/2.3 version so mighty, will inevitably be people abandoned. Not only sad from the heart, grief.
Android Tabactivity's sigh