【问题标题】:How to setResult from within a PreferenceFragment?如何从 PreferenceFragment 中设置结果?
【发布时间】:2018-06-11 11:47:23
【问题描述】:

我使用 Android Studio 向导创建了一个“设置”活动。在这个活动中是以下 PreferenceFragment 类..

public static class MyPreferenceFragment extends PreferenceFragment
{
...
}

我在这个 PreferenceFragment 类中有一些侦听器,我想从 PreferenceFragment 类中设置结果()。但是我遇到了“找不到方法异常”的崩溃。

如何访问活动/setResult?

【问题讨论】:

  • 发布您的日志猫。哪个方法没找到?

标签: android android-fragments preferencefragment


【解决方案1】:

片段不能有“结果”,你应该在活动中调用它。

你可以在任何你想设置结果的地方这样做:

Activity activity = getActivity();
//activity in fragment is nullable, 
//so null check is suggested to avoid NullPointerException
if (activity != null) {
    activity.setResult(RESULT_OK);
    activity.finish();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多