我有一个使用 Preferences UI 的 Android 应用程序。我希望能够深入链接到特定的偏好选项。我不知道该怎么做。
我已经使用教程设置了首选项,如下所示
- https://developer.android.com/guide/topics/ui/settings
- https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-1-5959aa49337c(第 1 部分)
- https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-2-2ba63e2d7d1d
- 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 的深层链接。只有一项活动,但有三种偏好。