android - 从 Android 偏好 Activity 跳转到 Activity

标签 android android-intent gradle

我有一个带有 PreferenceActivity 的 Android 应用程序,其中一个首选项条目启动了我自己的 Activity 之一(“关于”屏幕)。该条目如下所示:

<PreferenceScreen android:title="@string/about.Title">
    <intent ... />
</PreferenceScreen>

此外(这很重要),我使用 Gradle 和 .debug我的调试版本的应用程序 ID 后缀。我在手机上安装了 Play Store 版本和调试版本。

问题是:上面使用什么样的 Intent ?我知道有两种选择,但都不起作用:

  1. 使用android:action Intent 使用我自己的操作字符串,并使用 <intent-filter> 在我的 list 中注册该操作字符串.
    1. 如果我使用 android:exported="false"在 list 中,当安装两个应用程序时, Activity 无法在 Android 7.1 上加载。 (我得到“使用完成操作......没有应用程序可以执行此操作”。)如果只安装一个(任一)它工作正常。
    2. 如果我使用 android:exported="true" ,那么我不仅导出一个内部 Activity ,而且当我点击首选项时, Activity 的两个实例被推送到堆栈上(每个应用程序一个)。如果我只安装这两个应用程序之一,那么只会显示一个实例。
  2. 使用android:targetPackageandroid:androidClass方法,但是我不知道该包使用什么,因为它对于发布和调试版本是不同的(因为后缀)。一种解决方案是拥有 preferences.xml 的副本调试资源文件夹中的文件,只更改了包,但这很容易出错。

多年来我一直在使用选项 #1.1,但它最近在我的手机上坏了,我怀疑 7.1 更新是罪魁祸首。我以前从未遇到过这两个已安装应用程序之间的串扰!

这一定是一个很常见的模式!有什么想法吗?

最佳答案

请按照以下步骤操作:

使用添加首选项后

addPreferencesFromResource(R.xml.preferences);

找到你想要设置 onClick 的首选项并通过转换来定义它

Preference pref = (Preference) findPreference("pref");

然后你可以轻松地设置它的 onClick 使用:

pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference preference) {
        startActivity(new Intent(CurrentActivity.this, NextActivity.class));
        return true;
    }
});

关于android - 从 Android 偏好 Activity 跳转到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44450211/

相关文章:

android - 如何从我的应用程序启动 Android 中另一个已安装的应用程序?

android - Nativescript gradle 项目级

jenkins - SonarQube 未在 Gradle 多项目中显示 JUnit 测试的测试 Jacoco 覆盖率

iphone - Android/iOS API 访问蓝牙心率监视器

android - 窃听(聚焦)音频流android

android - 从 ADB shell 启动共享 Activity

Android 从另一个 Activity 更改 TextView

android - android.testVariants 有什么用?

android - 根据 Json 的响应更改 onClick 适配器中的图像

android - TextView 和 WebView 哪个更好?