preface

I have been developing Android for a few years, and I often feel the urge to write my own code when I think of it as a beginner. Therefore, I want to summarize the unreasonable place in the previous code, hoping to give some help to beginners. I hope this is a series of articles.

This section

What does a “universal” Activity look like, and what are the disadvantages of a “universal” Activity

Start writing “universal” activities

As a beginner, there may be a lot of friends write Activity is very “omnipotent”, of course not better. Then I will take a login module as an example, to say how a “universal” Activity is generated, the following code is pseudo-code.

To write the activity_login. XML file, simply write the pseudocode:

An EditText for the user account, an EditText for the password, and a login ButtonCopy the code

Write a LoginActivity that initializes views in activity_login. XML and adds a listener to the login button. Here’s a key snippet

public LoginActivity extends Activity{ private EditText mUserNameView, mPasswordView; private Button mLoginView; // This method is called by the onCreate method public void initViews(){....... All kinds of the findViewById... Code / / add listeners to login button mLoginView. An OnClickListener (new View. An OnClickListener () {@ Override public void onClick (View v) {}}); }}Copy the code

The current LoginActivity code is still clean and clean. It only does uI-related work.

The LoginActivity with the authentication function then implements the function to verify that the account and password are valid in the listener method of the login button, and continues to refine the code

mLoginView.OnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String userName = mUserNameView.getText(); String password = mPasswordView.getText(); // Verify that the user name and password are valid if(! validate(userName) || ! Validate (password)){tell the user that the entered username or password is invalid and do something else}}}); Private Boolean validate(String STR){} Private Boolean validate(String STR){}Copy the code

Now that the LoginActivity has business code, it’s time to implement the login function.

LoginActivity with login functionality adds login functionality to listeners

MLoginView. An OnClickListener (new View. An OnClickListener () {@ Override public void onClick (View v) {validate user input user name and password are legal code... If (both valid) {// start login(userName,password); }}}); Private void login(String userName,String password){private void login(String userName,String password){ Httpclient.getinstance ().login(userName,password, new ResponseListener(){public void failed(failed failed){ } public void success(Response, Response){public void success(Response, Response){public void success(Response, Response)}}); }Copy the code

LoginActivity with data parsing and storage functions When I completed the login function, this time the product raised new requirements, need to return the user related data after the user login successfully, and save. There is no choice but to follow the instructions, but fortunately the requirements are simple and you just need to modify the login success method.

Public void success(Response Response){public void success(Response Response){public void success(Response Response){public void success(Response Response){ Assuming response.getContent() exists String jsonContent = response.getContent(); JsonObject jsonObject = new JsonObject(jsonContent); UserInfo userInfo = new UserInfo(); userInfo.name = jsonObject.optString("name"); userInfo.userId = jsonObject.optString("userId"); Parsing of other fields...... // Save the userInfo information to the table, assuming that userDatabase already exists userdatabase.save (userInfo); Skip to app home page}Copy the code

At this point, the login function is developed, and the final code for the LoginActivity looks like this

public LoginActivity extends Activity{ private EditText mUserNameView, mPasswordView; private Button mLoginView; public void initViews(){ ....... All kinds of the findViewById... Code / / add listeners to login button mLoginView. An OnClickListener (new View. An OnClickListener () {@ Override public void onClick (View v) {String userName = mUserNameView.getText(); String password = mPasswordView.getText(); // Verify the password entered by the user if(! validate(userName) || ! Validate (password)){validate(password)){else{// Start login(userName,password); }}}); Private void login(String userName,String password){private void login(String userName,String password){ Httpclient.getinstance ().login(userName,password, new ResponseListener(){public void failed(failed failed){ } public void success(Response, Response){public void success(Response, Response){public void success(Response, Response){ Assuming response.getContent() exists String jsonContent = response.getContent(); JsonObject jsonObject = new JsonObject(jsonContent); UserInfo userInfo = new UserInfo(); userInfo.name = jsonObject.optString("name"); userInfo.userId = jsonObject.optString("userId"); Parsing of other fields...... // Save the userInfo information to the table, assuming that userDatabase already exists userdatabase.save (userInfo); Skip to app home page}}); } private Boolean validate(String STR){}}Copy the code

Now that the LoginActivity module function has been developed, the LoginActivity has changed from a clean and clean code that only cares about U to a “universal” Activity. What does the LoginActivity do?

  • Handle UI
  • Authenticate user names and passwords
  • Deal with data parsing, data storage

Of course, you can say that the current LoginActivity code is only about 100 lines, which is easy to modify, but as the business grows, who can guarantee that the LoginActivity code will not grow and contain more business code. LoginActivity is just a microcosm of a “universal “Activity.

Omnipotent is good thing, but must see omnipotent is to pass what means to realize, if be to pass a lot of small function disperses in respective kind, next omnipotent kind passes these small kind combination together omnipotent is true material solid material omnipotent. It is not good to make a class “omnipotent” by putting so many small functions into it.

Disadvantages of “universal” Activities LET me summarize the disadvantages of “universal” activities

  • There are a number of variables that cause a “universal “Activity to change, so you have to change it at every turn, and there is no way to minimize the chance of bugs being introduced by changes.
  • Business functions cannot be reused
  • Such activities are not maintainable, readable, and highly coupled.

Therefore, for long-term consideration, we must be bold in reconstructing “universal” activities. Although reconstructing takes a lot of time at present, it will be done once and for all in the future. Therefore, it is enough to make “universal” activities do only work related to THE UI.

conclusion

That’s all about the “universal” Activity. I’ll talk to you about how to reconstruct the “universal” Activity. Please look forward to……

Welcome everyone to exchange more, wrong place more advice. My contact information is in my profile.

The code is clean, clean, and refreshing to read. You can read 100 lines in one sitting!