App's red dot reminder three stages

Source: Internet
Author: User

The following page is the main screen after we enter the app. The number on the red dot of the customer option is to show the total number of customers we didn't see.



When we switch to customer this fragment, we show the number of loan customers and the number of insurance customers.



When we click on an option, if you enter into the activity of the insurance customer, the ListView displays the user's information, which is labeled red dot, which represents the number of customers who have not been viewed and who have not seen the red dot.


When we click on a customer message with a red dot, we need to send the message back, which means I have already viewed the message. and change the number in the main activity, and the number of frament. Such as:



So how does this function and how to optimize, I in the implementation of this function, the experience of the three stages.
The first stage:
Ideas: Using Sharedprefrence, and broadcasting.
Implementation: When we enter the main activity, read the data from the background to the total number of customers, the total number of insurance customers, the total number of loan customers, and store it in Sharedprefrence. The main activity is then read the total number of sharedprefrence customers and show up.
When we switched to frament, we read the number of loans and insurance users sequentially from Sharedprefrence and presented it.
When we click on the item with the red dot in the ListView, change the number of customers in Sharedprefrence and the number of insurance customers, send the broadcast, notify the change. The primary activity and fragment then read the data once from the sharedprefrence and then change.

We find that all operations in this process are used to sharedprefrence, and that the code is written in a variety of activities, that the code is bloated, the code is not reusable, and there is no logic. Then evolved the second way of thinking.

Second Stage
Idea: Use static data to have the same life cycle as the application, and discard the sharedprefrence.
Implementation: Create a class, in the class set 3 static Integer, respectively, the total number of customers, the number of insurance customers, the number of loan customers. Write three methods in this class, respectively. According to the number of customers to judge the red dot method, according to the number of insurance customers red dot method, according to the number of loan customers red dot method.
When we enter the main activity, we put the obtained data into 3 static quantities respectively. The red dots are then called according to the number of customers in the main activity.
When we enter the fragment, we call the red dot method according to the number of insurance clients, according to the number of customers in the credit method. Judging the situation of the insurance client and the credit client red dot display.
When we click on the ListView to display the item with the red dot, change the static total number of customers, the number of insurance customers, the number of loan customers. Send broadcast to main activity and fragment. Then the main activity calls the method of judging the red dot according to the number of clients, and calls the red dot method according to the number of loans, or according to the number of insurance customers in fragment.
The second way to improve the reuse of the code, and the use of a central class to control all operations, discard the frequent use of sharadprefrence, according to my test, but also to improve the speed of the red dot display.

Phase Three
Idea: Don't broadcast, take advantage of the life cycle of fragment and the life cycle of activity.
Specific implementation:
Expand on the second way of thinking and continue to use a central class to control all operations.
When we click on the item with the red dot in the ListView. Only change the number of the corresponding red dots without sending the broadcast. Instead, the fragment uses the method called in the Oncreatview () method and activity in the Onresume () method to determine the red dot display.
We know that when we jump from fragment, we execute the Ondestroyview () method in Fragmeng, and when we switch back to fragment, we execute the Oncreatview () method.
The OnStop () method is executed when jumping to the activity, and the Onresume () method is executed when it jumps back.
The third idea is to take full advantage of the life cycle, discarding the use of the broadcast and simplifying the code.

In the whole process we found that a good idea is in the process of doing your project slowly come out, not our step to get. The first thing we need to do with what we can think of is the ability to do this, and on top of that, in the optimization of code, especially in the development of actual projects, speed first, performance second. The best strategy is to quickly complete the project and then iterate quickly to optimize performance and code.

App's red dot reminder three stages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.