1
\$\begingroup\$

I have an app that has only one activity and 5 fragments. I removed the navigation component because it was buggy and restored the old navigation, which is simply a BottomNavigationView with OnClickListener.

The problem is, that I don't want to recreate the fragment views, because it takes up to a second for 2 of them (and also I don't want that the webview is recreated, because it would have to load the page again). Thats why I just simply hide/show them. And here is the first problem: They are still running in background and recieve events, etc. what I don't want.

But the real problem is performance: I set slide in/out transitions and they lagg, while opening a fragment with only 1x RecyclerView and 10 items in it. The items only contain 4 strings...

I have a fragment with a WebView where it is much worse. The thing is, when I open the fragments the first time it still takes a second, because it has to create the view, but after that, it only has to animate the old fragment out and the new one in. Why does it skip frames? The UI thread is overwhelmed with just the animation!?! When I remove the animation, it nearly changes fragments instantly, maybe there is like 30ms delay (not even worth mentioning) and it doesn't look laggy.

I am new to android and I saw that there are a lot of ways of handling fragments and navigation, what is the best practice to display fragments, save their state and animate them smooth? Or is there even an alternative to fragments?

I don't need working code, just a step in the right direction.

How I am currently changing fragments:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

//Set animations
if(activeFragment < index){
    ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
} else {
    ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
}

//Hide old Fragment
if (Fragments.get(activeFragment).isAdded())
{
    ft.hide(Fragments.get(activeFragment));
    Fragments.get(activeFragment).getView().setTranslationZ(0);
}

//Add/Show new Fragment
if (Fragments.get(index).isAdded()) { // if the fragment is already in container
    ft.show(Fragments.get(index));
    Fragments.get(index).getView().setTranslationZ(1f);
} else { // fragment needs to be added to frame container
    ft.add(R.id.fragment_container, Fragments.get(index), Integer.toString(index));
}

// Commit changes
ft.commit();
\$\endgroup\$

0

Browse other questions tagged or ask your own question.