Explain in detail how to implement suspended activity in Android

Generally speaking, it is not difficult to develop a tablet version for an optimized mobile app. Using components such as fragment and decompose entities can be completed quickly and easily. However, recently encountered a project is not so simple. Not only do we need to develop a tablet app (based on actionbar Sherlock library, also known as ABS), we also need to realize the following functions:

As we said before, there is already a well-developed mobile app, so it would be a waste of time to redesign and re code it. The following explanations and descriptions are based on the existing code. By modifying the implementation of activity to adapt to the tablet device, the maximum utilization of the code has been achieved.

Task overview

There are three main tasks:

Calculate window size

As mentioned earlier, we need to build on an existing mobile app (using the actionbar Sherlock Library), which we have integrated into the project in the form of an external library with source code.

If you are familiar with the source code of ABS or understand the architecture of the native actionbar, you will find that if you change the size of the activity, the function will not be realized when calling the setcontentview method: the size and location of the actionbar will not change. At this time, we need to operate at a higher level. When the system draws the actionbar - ABS or the native actionbar, there is no data in any window, so we can adjust it as needed.

The most obvious way is to change the size of the window, so here you need the following code:

After the code is executed, you can see that the size of the window has changed according to the values of M activitywindowwidth and m activitywindowheight.

is_ The tablet check is only valid for the tablet. The openassmallwindow ID indicates whether the window is displayed in full screen or rendered as a new window.

If an error is reported during operation, the error log is: actionbarview can only be used with Android: layout_ Width = "match_parent" (or fill_parent). Don't worry. We have the source code of ABS. Just modify the source code directly.

Open the actionbarview file, and then comment out the thrown exception in the onmeasure method -- this should solve the problem. Android uses a special permission control mechanism, so we can continue to go deep here. Maybe we can roughly calculate menu items, but we can't control many things. Note: this method is not tested on the native actionbar, because it has only been tested in the author's Android version. If you want to disable actionbar, you need to find the actionbarsherlock class and comment out the following line of code:

The code should run without problem, and then you can see the activity with opaque background.

Add transparency

After the size and size of the activity are changed, it is now time to make it transparent. You can add this property to the activity theme on this tablet:

This property value makes the activity background transparent.

It looks good. The effect has been basically realized.

Another small problem: all the above tests are executed on nexus7 Android 4.3, but the activity of this app is not displayed in the Android 4.2 version of nexus7.

After many times of debugging and log checking, we found that our activity (called mainactivity) was not destroyed (the ondestroy method was not called), so the Android system ignored it and did not draw it. Why? Because Android 4.4 KitKat adds an optimization algorithm. Because of this optimization algorithm, the Android system will draw the mainactivity when it sees that the activity is in full screen display mode (regardless of the change of WindowManager), but the things on it are ignored, so we can't see any display.

After studying this problem, we found that dialog boxes and other parts not displayed in full screen can be drawn normally, so we need to add these lines to the theme theme:

So the problem is solved. To sum up, we have implemented a suspended and transparent activity.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>