Hello.

I want to say in advance that the EventBus library itself has not yet been used, but a question arose about the expediency of using it.

What is:

Activity with ViewPager embedded in TabLayout with 2 snippets. On each fragment is on RecyclerView . Information for them is taken from ArrayList'ов , which are updated by Google Firebase ValueEventListener'у .

What it looks like now:

At the beginning, a SectionsPagerAdapter declared, which initializes 2 fragments. All of them are in the same class. Inside each fragment in the onCreateView method, RecyclerView and its adapter are initialized. Data is taken from private static ArrayList'ов . The adapters themselves are also declared private static . Inside ValueEventListener'а these ArrayList'ы and adapters are updated accordingly. private static searchView and FragmentManager private static searchView the same heap, because They are needed to work inside the fragments.

As for me, it all looks and works terribly. In this regard, I want to know the opinion on this matter:

  • Is it worth reworking using EventBus (and is it possible)
  • Is it worth it to alter, using any standard methods (and what)
  • Keep it as it is with memory leaks

Thank you in advance for your response.

Cut class code:

 public class UserActionsActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{ private static Context context; private ViewPager viewPager; private SectionsPagerAdapter mSectionsPagerAdapter; private static RecyclerView unverifiedUsersView; private static RecyclerView usersView; private static DatabaseReference databaseReference; private ValueEventListener valueEventListener; private static ArrayList<User> unverifiedUsersList = new ArrayList<User>(); private static ArrayList<User> usersList = new ArrayList<User>(); private static UnverifiedUserRecyclerAdapter adapter; private static UserRecyclerAdapter adapter1; private static FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_actions); context = UserActionsActivity.this; initializeComponents(); setEvents(); } private void initializeComponents(){ fragmentManager = getSupportFragmentManager(); databaseReference = FirebaseDatabase.getInstance().getReference(); mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); viewPager = (ViewPager) findViewById(R.id.viewPager); viewPager.setOffscreenPageLimit(2); viewPager.setAdapter(mSectionsPagerAdapter); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); } private void setEvents() { valueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (!search) { unverifiedUsersList = Globals.Downloads.getSpecificVerifiedUserList(dataSnapshot, DatabaseVariables.Users.DATABASE_UNVERIFIED_USER_TABLE); adapter = new UnverifiedUserRecyclerAdapter(getApplicationContext(), unverifiedUsersList); unverifiedUsersView.setAdapter(adapter); adapter.notifyDataSetChanged(); usersList = Globals.Downloads.getVerifiedUserList(dataSnapshot); adapter1 = new UserRecyclerAdapter(getApplicationContext(), usersList); usersView.setAdapter(adapter1); adapter1.notifyDataSetChanged(); } } @Override public void onCancelled(DatabaseError databaseError) { } }; } public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { if (position == 0) return FirstFragment.newInstance(); else return SecondFragment.newInstance(); } @Override public int getCount() { return 2; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "Не авторизованные"; case 1: return "Все"; } return null; } } public static class FirstFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_recycler, container, false); unverifiedUsersView = (RecyclerView) v.findViewById(R.id.recycler); LinearLayoutManager mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);; adapter = new UnverifiedUserRecyclerAdapter(context, unverifiedUsersList); unverifiedUsersView.setLayoutManager(mLayoutManager); unverifiedUsersView.setHasFixedSize(false); unverifiedUsersView.setAdapter(adapter); adapter.notifyDataSetChanged(); return v; } public static FirstFragment newInstance() { FirstFragment f = new FirstFragment(); return f; } } public static class SecondFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_recycler, container, false); usersView = (RecyclerView) v.findViewById(R.id.recycler); LinearLayoutManager mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);; adapter1 = new UserRecyclerAdapter(context, usersList); usersView.setLayoutManager(mLayoutManager); usersView.setHasFixedSize(false); usersView.setAdapter(adapter1); adapter1.notifyDataSetChanged(); return v; } public static SecondFragment newInstance() { SecondFragment f = new SecondFragment(); return f; } } } 

    2 answers 2

    Why do you have so many static fields? Because of this, and rather flows. Here EventBus does not help. Yes, and in my opinion eventbus can be called anti-fame.

    • Well, in fact, the question is how to get rid of this outrage. - ahgpoug
    • Then attach the code - pavel163
    • Added code. I understand that this is all very crookedly done, but I could not find a better solution. - ahgpoug
    • And why do you have all static fields? Do you understand how it works at all? Take out all the classes nested. Remove statics. Create a method in fragments to add data. pass the data into the fragment through this method. Update when it comes to update the list. - pavel163
    • It turns out that I create a fragment, using the method, I get the necessary data for it from the activation method, and then I return from it a link to RecyclerView to work in the main activation? - ahgpoug

    Usually I do not consider myself to be stupid people, but in this matter I was a complete fool until Mr. pavel163 brought me to the right idea. Again, I don’t know how correct this is, but I implemented it this way:

    Brought all fragments and SectionsPagerAdapter in a separate class. It looks like this:

     public class UserActionsFragments { public static class SectionsPagerAdapter extends FragmentPagerAdapter { private FirstFragment firstFragment; private SecondFragment secondFragment; public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { if (position == 0) return FirstFragment.newInstance(); else return SecondFragment.newInstance(); } @Override public int getCount() { return 2; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "Не авторизованные"; case 1: return "Все"; } return null; } @Override public Object instantiateItem(ViewGroup container, int position) { Fragment createdFragment = (Fragment) super.instantiateItem(container, position); switch (position) { case 0: firstFragment = (FirstFragment) createdFragment; break; case 1: secondFragment = (SecondFragment) createdFragment; break; } return createdFragment; } public void updateFirstFragment(%параметры%){ firstFragment.updateContent(%параметры%); } public void updateSecondFragment(%параметры%){ secondFragment.updateContent(%параметры%); } } public static class FirstFragment extends Fragment { RecyclerView unverifiedUsersView; boolean result; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_recycler, container, false); unverifiedUsersView = (RecyclerView) v.findViewById(R.id.recycler); return v; } public void updateContent(%параметры%) { LinearLayoutManager mLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false); unverifiedUsersView.setLayoutManager(mLayoutManager); unverifiedUsersView.setHasFixedSize(false); UnverifiedUserRecyclerAdapter adapter = new UnverifiedUserRecyclerAdapter(context, unverifiedUsersList); unverifiedUsersView.setAdapter(adapter); adapter.notifyDataSetChanged(); } public static FirstFragment newInstance() { FirstFragment f = new FirstFragment(); return f; } } //аналогично для 2 фрагмента } 

    And at the moment I need (for example, when updating to a ValueEventListener ) I refer to the mSectionsPagerAdapter , for example

     mSectionsPagerAdapter.updateSecondFragment(usersList, UserActionsActivity.this); 

    I do not know how correct this is, but as a result, there is not a single static field left in the Activity. For me personally, this is a victory. Poke me with a stick if I did something terrible again, until I can accept my answer for another 22 hours.

    The only embarrassment is that you have to install the LayoutManager every time.