KarlsMaranjs
KarlsMaranjs

Reputation: 535

Android - Setting activity inside a fragment is empty

I'm creating an app that contains 5 tabs in the bottom navigation menu. One of these tabs contains a default settings activity that I created with Android Studio. The onClick event on every tab displays a different fragment. When I click on the "Settings" button the fragment for the settings activity is loaded with no errors, however it is empty. I've looked elsewhere, but still haven't found an answer. My problem is not about displaying a group of settings in a different fragment, rather generating for the first time the Settings fragment itself.

I listen for the click event in the main activity. Then I load the right fragment class, this class calls the .SettingsActivity class that generates the view using settings_activity.xml and loads the "settings" from root_parameters.xml. I've changed nothing from the default activity created by Android Studio. And I'm loading it the same way I load the other fragments and they all work, including a Google Maps fragment.

This is my .MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);

    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}

private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()) {
                    case R.id.map_nav:
                        selectedFragment = new MapFragment();
                        break;
                    case R.id.settings_nav:
                        selectedFragment = new OldSettingsFragment();
                        break;
                    case R.id.user_nav:
                        selectedFragment = new UserFragment();
                        break;
                    case R.id.test_settings_nav:
                        selectedFragment = new SettingsFragment();
                        break;
                    default:
                        selectedFragment = new HomeFragment();
                        break;
                }

                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();

                return true;
            }
        };

}

Here is my .SettingsFragment

public class SettingsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    return inflater.inflate(R.layout.settings_activity, container, false);
}

}

This is the .SettingsActivity as created by Android Studio

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.settings, new SettingsFragment())
            .commit();
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

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

}

This is settings_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".TestSettings.SettingsActivity">

    <FrameLayout
        android:id="@+id/settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

And finally this is my root_preferences.xml

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

<PreferenceCategory app:title="@string/app_name">

    <EditTextPreference
        app:key="signature"
        app:title="@string/signature_title"
        app:useSimpleSummaryProvider="true" />

    <ListPreference
        app:defaultValue="reply"
        app:entries="@array/reply_entries"
        app:entryValues="@array/reply_values"
        app:key="reply"
        app:title="@string/reply_title"
        app:useSimpleSummaryProvider="true" />

</PreferenceCategory>

<PreferenceCategory app:title="@string/sync_header">

    <SwitchPreferenceCompat
        app:key="sync"
        app:title="@string/sync_title" />

    <SwitchPreferenceCompat
        app:dependency="sync"
        app:key="attachment"
        app:summaryOff="@string/attachment_summary_off"
        app:summaryOn="@string/attachment_summary_on"
        app:title="@string/attachment_title" />

</PreferenceCategory>

PLEASE HELP.

Upvotes: 1

Views: 997

Answers (2)

Zain
Zain

Reputation: 40878

You can not set an Activity in one of BottomNavigationView tabs instead of a fragment.

So, you need to replace SettingsActivity with a Fragment, I will name it as PreferenceFragment as you already has an inner SettingsFragment.

So modify your SettingsActivity with:

public class PreferenceFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        
        ActionBar actionBar = requireActivity().getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        return view;
    }

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

}

So, now delete settings_activity.xml and you need to create fragment_settings.xml instead to have a static fragment for the inner SettingsActivity Fragment

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/settings_root"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/settingsFragment"
        class="com.xx.PreferenceFragment$SettingsFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Note: replace the com.xx with your package name that holds the PreferenceFragment

Upvotes: 1

Jyotish Biswas
Jyotish Biswas

Reputation: 574

You are loading the SettingsFragment on test_settings_nav click which inflate R.layout.settings_activity and the entire layout has a FrameLayout under a LinerLayout and you don't load anything in that FrameLayout so its Empty. Load something on that frame layout will appear in screen.

Upvotes: 0

Related Questions