Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Android use EventBus to receive messages multiple times

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how Android uses EventBus to receive messages many times". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how Android uses EventBus to receive messages many times" can help you solve the problem.

Scene

The Fragment is nested in the Fragment, and the nested Fragment is reused. Click item to enter the details. After returning, you need to return the data and refresh the list.

Because of the nesting between Fragment, it is not convenient to use Intent to return data, and it is not convenient to use callback callback to return data, so I chose to use EventBus to send messages, but found that after using EventBus to send messages in the list, the method of receiving messages in Fragment will be executed multiple times.

Send a message:

EventBus.getDefault () .post (new TestEvent ()

Receive messages:

@ Subscribe (threadMode = ThreadMode.MAIN) public void onTestEvent (TestEvent event) {onSwipeRefresh ();}

That is, the onTestEvent method will be called multiple times and the data will be refreshed multiple times.

Cause analysis

Because the Fragment is reused and the EventBus subscription message is in the reused Fragment, the message is subscribed multiple times. Take ItemFragment as an example.

Public class ItemFragment extends Fragment {private static final String ARG_COLUMN_COUNT = "column-count"; private int mColumnCount = 1; public ItemFragment () {} @ SuppressWarnings ("unused") public static ItemFragment newInstance (int columnCount) {ItemFragment fragment = new ItemFragment (); Bundle args = new Bundle (); args.putInt (ARG_COLUMN_COUNT, columnCount); fragment.setArguments (args); return fragment } @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); if (getArguments ()! = null) {mColumnCount = getArguments () .getInt (ARG_COLUMN_COUNT);}} @ Override public void onStart () {super.onStart (); / / subscription message EventBus.getDefault () .register (this) } @ Override public void onStop () {super.onStop (); / / unsubscribe to EventBus.getDefault () .unregister (this);}}

Reuse ItemFragment:

Public class HomeFragment extends Fragment {private int activeColor = Color.parseColor ("# ffffff"); private int normalColor = Color.parseColor ("# 666666"); private final String [] tabs = new String [] {"first page", "second page", "third page", "fourth page", "fifth page"} Public View onCreateView (@ NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {... Vp2.setAdapter (new FragmentStateAdapter (getActivity (). GetSupportFragmentManager (), getLifecycle ()) {@ NonNull @ Override public Fragment createFragment (int position) {return ItemFragment.newInstance (position + 10);} @ Override public int getItemCount () {return tabs.length;}});.}

Because five ItemFragment objects are created, the EventBus is subscribed five times, so the message is also received five times.

Solution

1. Subscribe to EventBus messages in the host Activity in advance, but this method is not suitable for multi-level Fragment nesting. If the EventBus subscription is not successful, the message will not be received.

2. Add tags to the Fragment to identify the current Fragment.

@ Override public void onStart () {super.onStart (); / 2 is the index if (2 = = position) {EventBus.getDefault () .register (this) of the Fragment that needs to receive the message;}} @ Override public void onStop () {super.onStop (); if (2 = = position) {EventBus.getDefault () .index (this) }}

In the reused Fragment, only the Fragment with index 2 will subscribe to the message, so only the Fragment with index 2 can receive the message when sending the message, which solves the problem of receiving messages many times.

3. Add tags to the message.

@ Subscribe (threadMode = ThreadMode.MAIN) public void onTestEvent (TestEvent event) {if (event! = null) {/ / 2 is the index if of the Fragment that needs to receive the message (event.getPosition () = = 2) {onSwipeRefresh ();}

EventBus sends the position of which page refresh is needed when the post message is sent, and identifies which page refresh is needed according to the index when receiving the message. You don't have to have the position of Fragment, as long as you can uniquely identify Fragment.

This is the end of the introduction to "how Android uses EventBus to receive messages many times". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report