Problem leads to
// Create dialog builder=new (); // Create dialog method two =new (getApplicationContex());
The difference is that when constructing an application is passed on to a current activity. The second method reports an error. So what is the difference?
Dialog shares a WindowManager object with Activity, so Activity shares the same mAppToken value as Dialog. So when creating Dialog, if the passed Context is Application
Dialog source code analysis
Construction method
There are three more important functions in the construction method, you can check it in the source code yourself
// Step 1. Get the WindowManager object mWindowManager = (WindowManager)(Context.WINDOW_SERVICE); // Step 2. Create a new Window for Dialog Window w = (mContext); mWindow = w; // Step 3. Associate WindowManager with the new Window // Note: The second parameter token is null, that is, if a Window belongs to Dialog, then the mAppToken object passed to the Window is null, and Dialog does not have its own token (mWindowManager, null, null);
- Step 1: Because context is Activity, the obtained WindowManager belongs to Activity, so ** Dialog and Activity share a WindowManager object**
- Step 2: After obtaining the WindowManager object of Activity, Dialog creates a new Window object (PhoneWindow type, the creation process is similar to the Window creation process of Activity)
- Step 3: Associate the newly created Dialog's window to the WindowManager of the Activity. It is particularly important to note that: Regarding AppToken, the mAppToken object passed to Window is null, but it does not mean that Dialog's window has no token.
So come to a conclusion
- Conclusion 1: Dialog shares a WindowManager object with Activity
- Conclusion 2: Dialog has its own window Window (PhoneWindow type)
- Conclusion 3: Dialog's Window is managed uniformly by the affiliated WindowManager object
show() method
// 1. Call Dialog's onCreate() dispatchOnCreate(null); // 2. Call Dialog's onStart() onStart(); // 3. Get the DecorView object of the current new Window (similar to Activity) mDecor = (); // 4. Get the parameters of the new Window l = (); // 5. Add a View to the windowManager shared with Activity (mDecor, l);
- When Dialog obtains the DecorView object of the current new Window, the process is similar to Activity, so there is a way to customize Dialog layout, which is to override Dialog's onCreate method and use setContentView to pass in the layout, similar to Activity.
- Step 4: Since Dialog and Activity share a WindowManager object, Activity and Dialog share the same mAppToken value (just the Window objects of Dialog and Activity are different).
- Step 5: The addition process is consistent with the Activity window addition process.
This is all about this article about the Android Dialog window mechanism. For more related contents of Android Dialog window mechanism, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!