Making a PreferenceFragmentCompat Settings screen load faster (Navigation UI)

Viewed 180

Refering to this Settings Screen: (https://developer.android.com/guide/topics/ui/settings)

I've noticed a small lag when changing to the settings screen, which doesn't happen when changing to other fragments (when using a Navigation UI controlled Bottom Navigation Bar). I believe this is caused because the preferences xml file is loaded in a fragment at run time, since after changing fragments the lag goes out. Also this lag doesn't occur when changing to the other fragments.

This is my main Activity

public class MainActivity extends AppCompatActivity{

    NavController navController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navController = Navigation.findNavController(this, R.id.fragment_main);

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);  
        NavigationUI.setupWithNavController(bottomNavigationView, navController);

    }
}

The settings fragment

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);
    }
}

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:itemBackground="@color/bottomNavColor"
        app:itemIconTint="@color/bottom_nav_menu"
        app:itemTextColor="@color/bottom_nav_menu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/fragment_main"
        app:navGraph="@navigation/nav_view"
        app:defaultNavHost="true"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout="@layout/fragment_main_screen" />

</androidx.constraintlayout.widget.ConstraintLayout>

Bottom Navigation Manu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_fragment_main"
        android:icon="@drawable/ic_home_dark"
        android:title="Home"/>

    <item
        android:id="@+id/profileFragment"
        android:icon="@drawable/ic_profile"
        android:title="My Profile" />

    <item
        android:id="@+id/settingsFragment"
        android:icon="@drawable/ic_settings"
        android:title="Settings" />
</menu>

nav_view.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_view"
    app:startDestination="@id/navigation_fragment_main">

    <fragment
        android:id="@+id/navigation_fragment_main"
        android:name="com.example.drinkinggamesapp.Fragment_main"
        android:label="fragment_main_screen"
        tools:layout="@layout/fragment_main_screen" >
    </fragment>
    
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.example.drinkinggamesapp.SettingsFragment"
        android:label="SettingsFragment" />
    <fragment
        android:id="@+id/profileFragment"
        android:name="com.example.drinkinggamesapp.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" />
</navigation>

How could I reduce the lag when changing to the Settings Fragment? Can I somehow preload it and keep the NavigationUI? Anyone got experience with it?

0 Answers
Related