Changing between Navigation Drawer fragments is slow

  • Replies:28
  • OpenNot stickiedUnanswered
  • Forum posts: 258

Jun 4, 2016 10:41:02 PM via Website

What if all fragments will be initialized in onCreate() ?

HomeFragment homeFragment;
HalachaFragment halachaFragment;
other fragments...........

Your Activity {

protected override void OnCreate(Bundle bundle)
{ 
other strings ..........
homeFragment = new HomeFragment();
halachaFragment = new HalachaFragment();
other fragments...........
}

private void ListItemClicked(int position)
{

    switch (position)
    {
        case 0:
            fragment = homeFragment;
            Title = "Home";
            SupportActionBar.Elevation = 8;
            break;
        case 1:
            fragment = halachaFragment;
            Title = "aaa";
            SupportActionBar.Elevation = 0;
            break;
other cases..........
}
}
.....................
}

— modified on Jun 4, 2016 10:51:15 PM

  • Forum posts: 26

Jun 4, 2016 10:56:51 PM via Website

Interesting idea, although sadly the lag is still there in the second fragment :(

  • Forum posts: 26

Jun 4, 2016 10:58:16 PM via Website

Although it only happens on the first time it is clicked. Why is this?

  • Forum posts: 258

Jun 4, 2016 11:05:03 PM via Website

When you select other fragment, previous one is not destroyed, but only detached and when it is recalled the FragmentManager extracts it from the backstack.

public override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Create your fragment here
}

Maybe comment is right? :)

— modified on Jun 4, 2016 11:08:02 PM

  • Forum posts: 26

Jun 4, 2016 11:07:47 PM via Website

Oh, I see. If any other idea comes up, I'll be glad. If you want to see some of my other code, I can post it.

Thanks!

  • Forum posts: 258

Jun 4, 2016 11:14:41 PM via Website

I have edited my post and are not sure you read it after...

Maybe you need to init tab fragments in onCreate of HalachFragment? There is comment "Create your fragment here".

  • Forum posts: 26

Jun 4, 2016 11:25:03 PM via Website

Tried changing it to this:

public class HalachaFragment : Fragment
{
    private ViewPager halachotPager;
    private PagerSlidingTabStrip tabs;
    private Fragment[] fragments;
    TabsFragmentPagerAdapter tabsAdapter;

    public HalachaFragment()
    {
        this.RetainInstance = true;
    }
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        fragments = new Fragment[]
       {
           new HalachotFragment(),
           new BooksFragment(),
       };

        var titles = CharSequence.ArrayFromStringArray(new[]
       {
                "הלכות",
                "ספרים",
        });

        tabsAdapter = new TabsFragmentPagerAdapter(ChildFragmentManager, fragments, titles);
        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment

        var view = inflater.Inflate(Resource.Layout.HalachaSection, container, false);


        halachotPager = view.FindViewById<ViewPager>(Resource.Id.halachotPager);

        halachotPager.Adapter = tabsAdapter;
        halachotPager.OffscreenPageLimit = 2;
        halachotPager.SetCurrentItem(1, true);
        // Bind the tabs to the ViewPager
        tabs = view.FindViewById<PagerSlidingTabStrip>(Resource.Id.halachotTabs);

        tabs.SetViewPager(halachotPager);

        return view;

    }

Is this what you meant? If so, it doesn't help. :(

  • Forum posts: 26

Jun 5, 2016 7:44:49 PM via Website

I thought maybe it's a problem with my layout, so I'm posting my xml code:

Main:
image

HalachaSection (for HalachaFragment):
image

I know my layout is a bit messed up. If you could help me clean it a bit and maybe rearrange it, maybe that will help with the performance (also to solve the weird issue I have with the shadows which is why I need to change the Support Action Bar Elevation each time).

Thanks!

  • Forum posts: 79

Oct 6, 2016 2:28:12 PM via Website

Much appreciated. I figured out how to minimize the slack by equipment speeding up, evacuating movement and pointless code. Albeit notwithstanding when I don't put any picture in the second part (the one with the tabs), there is a little slack that doesn't happen when I tap on the others or when I open the same piece a second time.

Is there an approach to introduce the piece before opening it, so that once it is opened it will be stacked and won't slack?

Then again whatever other recommendations?

Much appreciated!