我有一个使用偏好界面的Android应用。我希望能够深入链接到特定的首选项。我不知道该怎么做。
我已按照以下说明使用本教程设置了首选项
我已使用本教程设置了深层链接活动
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的深层链接。活动只有一种,但有三种偏好。