android browser as many Windows solution details

  • 2020-05-17 06:29:04
  • OfStack

Our Android platform is composed of one and one Activity, and each Activity has one or more View.
So, when we want to display an interface, the first thing that comes to mind is to create an Activity, and then all the operations are implemented in Activity, or an Dialog or an Toast. This way is simple, but in some cases, we only want to show the simple, Activity is obviously redundant, at this time, how do we deal with it?

One application of Android is also a process of linux at the bottom level, but the concept of process is weakened at the top level to abstract such an interaction as Activity. The code directly controls Activity, and the user interaction is also Activity.
Activity is an object abstracted from the perspective of user interaction, separated from the process in concept and usage. A process can have multiple Activity, not just the Activity it currently USES,
You can also adopt Activity from other installers assigned to the process. Activity is destroyed, but the process is not destroyed (unless the system requires it or the code forces it to kill the process).

It turns out that the entire Android windowing mechanism is based on an interface called WindowManager, which adds view to the screen,
You can also remove view from the screen. It is aimed at the screen at one end and View at the other end, ignoring our previous Activity
Or Dialog or something like that. Actually our underlying implementation of Activity or Diolog is also via WindowManager, this
WindowManager is global, and the whole system is this one-only thing. It is the bottom layer showing View.

Write 1 simple code:
Java code


WindowManager mWm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);    
Button view = new Button(this);    
view.setText("window manager test!");    
WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();    
mWm.addView(view, mParams);   

When you first started developing android, you made the mistake of getting getWidth() and getHeight() in the constructor of View,
When an view object is created, android does not know its size, so getWidth() and getHeight() return 0,
The real size is only calculated when calculating the layout, so it's interesting to find out why you get the width and length at onDraw().

Suspension Windows are implemented using WindowManager


       WindowManager.LayoutParams params;
        params = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,//TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP;
        manager.addView(tmpView, params);  

You can add View to the window that needs to be added to the hover window:

 if(view.getParent==null)// if view Is not added to a parent component WindowManager In the 
        wManager.addView(view,wmParams);

Where, view is the view component that needs to be put into the hover window.

To remove it from WindowManager, execute the following statement:


        if(view.getParent()!=null)
        wManager.removeView(view);

In android, multiple Windows can be added according to the above method. Problems caused by multiple Windows:

2. Application lifecycle issues
When other applications appear before the browser master Activity, the browser lifecycle enters the onPause state regardless of how many browser child Windows appear in front of it.


Related articles: