如何深入链接到偏好设置用户界面

时间:2019-04-10 00:48:18

标签: android android-preferences android-deep-link

我有一个使用偏好界面的Android应用。我希望能够深入链接到特定的首选项。我不知道该怎么做。

我已按照以下说明使用本教程设置了首选项

  1. https://developer.android.com/guide/topics/ui/settings
  2. https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-1-5959aa49337c(第1部分)
  3. https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-2-2ba63e2d7d1d
  4. https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-3-ae9793fd31ec

我已使用本教程设置了深层链接活动

https://developer.android.com/training/app-links/deep-linking

我希望能够直接链接到其中一个首选项。

public class SettingsActivity extends AppCompatActivity
    implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
{
        @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_settings);
                FragmentFactory fragmentFactory = new AppFragmentFactory();

        final FragmentManager fragmentManager = getSupportFragmentManager();
        if (fragmentManager != null)
        {
            if (fragmentManager.findFragmentByTag(ROOT_FRAGMENT_TAG) == null)
            {
                final int containerId = R.id.detail_container;
                final Fragment fragment = fragmentFactory.create(RootPreferencesFragment.class);
                fragment.setArguments(savedInstanceState);
                fragmentManager.beginTransaction()
                    .add(containerId, fragment, ROOT_FRAGMENT_TAG)
                    .commit();
            }
        }
    }
}


    @Override
    public boolean onPreferenceStartFragment(PreferenceFragmentCompat callingFragment, Preference preference)
    {
        final Bundle arguments = preference.getExtras();
        final Fragment fragment = fragmentFactory.create(getClassLoader(), preference.getFragment());
        fragment.setArguments(arguments);
        fragment.setTargetFragment(callingFragment, 0);

        getSupportFragmentManager().beginTransaction()
            .replace(R.id.detail_container, fragment)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
            .setBreadCrumbTitle(preference.getTitle())
            .addToBackStack(preference.getKey())
            .commit();
        return true;
    }

RootPreferencesFragment

public class RootPreferencesFragment extends PreferenceFragmentCompat implements DialogFragmentCallback
{
    @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
{
    final FragmentActivity activity = Objects.requireNonNull(getActivity());

        // Notifications
    final Preference notificationsPreference = preferencesFactory.create(
        accountCategory,
        Preference.class,
        RootViewModel.ACCOUNT_NOTIFICATIONS_PREFERENCE_KEY,
        R.string.PushNotifications,
        RootViewModel.ACCOUNT_CATEGORY_KEY);
    notificationsPreference.setFragment(NotificationPreferencesFragment.class.getName());


    // General Category
    final PreferenceCategory generalCategory = preferencesFactory.create(
        preferenceScreen,
        PreferenceCategory.class,
        RootViewModel.GENERAL_CATEGORY_KEY,
        R.string.preferences_general_group);


    // About category
    final PreferenceCategory aboutCategory = preferencesFactory.create(
        preferenceScreen,
        PreferenceCategory.class,
        RootViewModel.ABOUT_CATEGORY_KEY,
        R.string.preferences_about_group);

    // About
    final Preference aboutPreference = preferencesFactory.create(
        aboutCategory,
        Preference.class,
        RootViewModel.ABOUT_PREFERENCE_KEY,
        R.string.preferences_about);
    aboutPreference.setSummary(aboutViewModel.getAppVersion());
    aboutPreference.setFragment(AboutPreferencesFragment.class.getName());
}
}

我不知道如何触发到特定NotificationsPreference的深层链接。活动只有一种,但有三种偏好。

0 个答案:

没有答案