Android: how to write a “universal” activity

preface

I've been developing Android for some years. I often think of the code I wrote when I was a beginner. I have the impulse to spray myself. The code is too slag. Therefore, I want to summarize the unreasonable places in the previous code, hoping to give some help to beginners. I hope this is a series of articles.

Contents of this section

What does a "universal" activity look like? What are the disadvantages of a "universal" activity

Start writing a "universal" activity

As a beginner, there may be many friends who write activity as "omnipotent". Of course, it is not better. Let me take a login module as an example to explain how a "universal" activity is generated. Most of the following codes are pseudo codes.

Write activity_ Login.xml file, here simply write the pseudo code:

Write loginactivity, which contains initialization activity_ The view function in login.xml also includes the function of adding a listener to the login button. Here are the key code fragments

Is the code in loginactivity still fresh and clean. It only does UI related work.

Loginactivity with validation

Then, the listener method of the login button should be used to verify whether the account and password are valid, and continue to improve the code

Now the loginactivity has business code. It's time for us to implement the login function.

Loginactivity with login function

Add login function in listener

Loginactivity with data analysis and storage functions

When I complete the login function, the product puts forward new requirements. After the user logs in successfully, the user related data should be returned and saved. There's no way. You can only do it according to the requirements, but fortunately, the requirements are very simple. You only need to modify the login success method.

At this point, the login function is developed. The last code of loginactivity is as follows

The function of this login module has been developed. Loginactivity has changed from a fresh and clean code that only cares about u to a "universal" activity. What has loginactivity done

Of course, you will say that the current loginactivity code is nearly 100 lines, which is easy to modify. However, with the growth of business, who can guarantee that loginactivity code will not be more and more, and will not contain more business codes. Loginactivity is just a microcosm of "universal" activity.

Omnipotence is a good thing, but it depends on how omnipotence is realized. If it is achieved by dispersing many small functions into their respective classes, then omnipotence is the omnipotence of real materials by combining these small classes. If you make this class "omnipotent" by putting many small functions in one class, this "omnipotent" is not good.

Disadvantages of "universal" activity

Let me summarize the disadvantages of "universal" activity

Therefore, for the long-term consideration in the future, we must boldly refactor the "universal" activity. Although the current refactoring takes a lot of time, it will be a long-term effort in the future. Therefore, it is enough to let the "universal" activity not be universal, and let the activity only do UI related work.

summary

That's all for the "universal" activity. Please see Android: reconstruction of "universal" activity

The code is fresh and clean. It's refreshing to read. It's no problem to read 100 lines at a time!

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of 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
分享
二维码
< <上一篇
下一篇>>