How to work with preference in single activity design using navigation component?

◇◆丶佛笑我妖孽 提交于 2021-01-28 06:20:27

问题


I want to migrate to single activity design using navigation component. I am using one activity and others are fragment. For some screens, I have only layouts, no preference. No problems to inflate those in fragment. But i faced problem when i try to work with preference.

My requirements is: I need to inflate a toolbar and preference list in a fragment. My approaches:

  1. Add preference using this following code.

SettingFragment.java

 @Override
     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
      setPreferencesFromResource(R.xml.setting_main, rootKey);
 }
  1. Configure for app bar for navigation component.

     @Override
     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
    
         mNavController = Navigation.findNavController(view);
         AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(mNavController.getGraph()).build();
         Toolbar toolbar = view.findViewById(R.id.the_toolbar);
    
         NavigationUI.setupWithNavController(toolbar, mNavController, appBarConfiguration);
     }
    
  2. In app theme, i used a layout for preference which contains toolbar and framelayout.

    <style name="Theme.MyApp" parent="@style/Theme.AppCompat.Light.NoActionBar">
     ...
      <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
    </style>
    
    <style name="PreferenceThemeOverlay">
       <item name="android:layout">@layout/fragment_settings </item>
    </style>
    

fragment_settings.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <androidx.appcompat.widget.Toolbar
        android:id="@+id/the_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:layout_gravity="bottom"
        android:layout_marginBottom="12dp"/>

       <FrameLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@android:id/list_container">
       </FrameLayout>

   </LinearLayout>

setting_main.xml

<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
   android:key="MyApp"
   android:title="My application">

  <androidx.preference.Preference
    android:icon="@drawable/ic_list1"
    android:key="PreferenceList1"
    android:title="PreferenceList1" />

  <androidx.preference.PreferenceCategory android:key="SecondCategory">

     <androidx.preference.Preference
        android:icon="@drawable/ic_list2"
        android:key="PreferenceList2"
        android:title="PreferenceList2" />

    </androidx.preference.PreferenceCategory>

</androidx.preference.PreferenceScreen>

My problems: If I do this, I can successfully add toolbar and preference. But i see a blinking issue in title during transaction from one fragment to another.

I have searched a lot, but did not find any suitable solutions. I am a newbie.

My questions:

  1. How can I solve this issue?

  2. Is there any way to work with preference in navigation component which also have a toolbar and preference in Fragment?

  3. For others fragment, which does not have preference i can inflate using this code which works fine with no blink issue. Is there any ways to do this for preference also adjusting with my requirements?

     @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 
      Bundle savedInstanceState) {
      return inflater.inflate(R.layout.setting_main_fragment, container, false);
    }
    

来源:https://stackoverflow.com/questions/65625479/how-to-work-with-preference-in-single-activity-design-using-navigation-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!