1

i am using bottom navigation and fragments to display data in my app but after i started loading data in my app the animation of fragments transition started to stuck and overall performance of app looks ugly I don't know how this happened, without the data fragments transition works just fine.

here is how i am loading data and calling fragments.

MainActivity.java

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        android.app.FragmentManager manager = getFragmentManager();
        switch (item.getItemId()) {
            case R.id.navigation_schedule:
                Fragment scheduleFragment = new ScheduleFragment();
                    manager.beginTransaction().replace(R.id.main, scheduleFragment).commit();
                return true;
            case R.id.navigation_teams:
                Fragment teamFragment = new TeamFragment();
                manager.beginTransaction().replace(R.id.main, teamFragment).commit();
                return true;
            case R.id.navigation_champions:
                Fragment championsFragment = new PointTableFragment();
                manager.beginTransaction().replace(R.id.main, championsFragment).commit();
                return true;
            case R.id.navigation_live:
                Fragment liveFragment = new LiveFragment();
                manager.beginTransaction().replace(R.id.main, liveFragment).commit();
                return true;
            case R.id.navigation_records:
                Fragment recordsFragment = new RecordsFragment();
                manager.beginTransaction().replace(R.id.main, recordsFragment).commit();
                return true;
        }
        return false;
    }
};

Fragment.java

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_team, null);
    lvTeam = (ListView)view.findViewById(R.id.lvTeam);

    DatabaseHandler db = new DatabaseHandler(this.getContext());
    listTeam = db.displayTeam();


    adapterTeam = new CustomAdapterTeam(this.getContext(),listTeam);
    lvTeam.setAdapter(adapterTeam);
    adapterTeam.notifyDataSetChanged();


    return view;
}

can anyone explain what makes the transitions looks like having glitches, it kind of sticks while transition is going on.

Eternal
  • 928
  • 9
  • 22

1 Answers1

4

Fragment transition is not smooth as much as it should be

First thing is Fragment transaction is smooth but you are doing some long operation in UI thread so UI get stuck. And you feel that fragment UI is not smooth as i should be.

  • I think your list have large amount of data which you are filling in once in UI thread. Instead of this you can use paging to load large data.
  • listTeam = db.displayTeam();, other thing can be, may be you are fetching data from database in UI thread. If you do that in AsyncTask. You will overcome this issue.

or you can post delay your long operation, so that data load after fragment gets opened. (Not recommended)

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_team, null);
    lvTeam = (ListView)view.findViewById(R.id.lvTeam);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    DatabaseHandler db = new DatabaseHandler(getContext());
                    listTeam = db.displayTeam();
                    adapterTeam = new CustomAdapterTeam(getContext(),listTeam);
                    lvTeam.setAdapter(adapterTeam);
                    adapterTeam.notifyDataSetChanged();
                }
            });
        }
    },100);
    return view;
}
  • Note that you can not do UI operation in worker thread.

Edit

Changed this.getContext() to only getContext(). Because this was referring to Runnable

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • hey thank you @Khemaraj but i am still having an error "can not resolve symbole this.getContext()". can you please help? – Eternal May 13 '18 at 10:05
  • You were getting error because `this` keyword refers to current object. (in that case was `Runnable`). remove `this`. – Khemraj Sharma May 13 '18 at 10:11