MrZaiko/Polysmee

View on GitHub
app/src/main/java/io/github/polysmee/settings/SettingsActivity.java

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
package io.github.polysmee.settings;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import io.github.polysmee.R;
import io.github.polysmee.settings.fragments.SettingsMainFragment;

/**
 *  Activity of the settings of the application.
 *
 *  Inspired from https://developer.android.com/guide/topics/ui/settings
 */
public final class SettingsActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.container_settings, new SettingsMainFragment())
                    .commit();
        }
    }

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
        // Instantiate the new Fragment
        final Bundle args = pref.getExtras();
        final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
                getClassLoader(),
                pref.getFragment());
        fragment.setArguments(args);
        // Replace the existing Fragment with the new Fragment
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container_settings, fragment)
                .addToBackStack(null)
                .commit();
        return true;
    }
}