如何从BasePreferenceActivity显示DialogFragment?

时间:2018-02-12 14:36:58

标签: android listview android-fragments android-preferences

我有一个扩展BasePreferenceActivity的SettingsPreferenceActivity,在该首选项列表中,我有一个名为Upgrade的项目。单击此升级项时,我需要显示一个DialogFragment,这是我的升级屏幕。这是我正在尝试使用的代码......

DialogFragment dialogFragment =
                            UpgradeDialogFragment.dialogWithEntry("leftMenuUpgrade");
                    dialogFragment.show(this.getSupportFragmentManager(), "purchase");

我收到错误消息“无法解析方法getSupportFragmentManager()”。

我也尝试过使用getFragmentManager(),我也遇到了同样的问题。

如何从BasePreferenceActivity中显示此DIalogFragment?

旧开发人员使用此代码......

                    Intent data = new Intent();
                    data.putExtra(EXTRA_UPGRADE, true);
                    setResult(RESULT_OK, data);
                    finish();

但它不起作用。什么都没发生,所以我试图为客户解决这个问题。

完整活动......

package com.tp.activity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceCategory;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.NavUtils;
import android.support.v4.app.FragmentManager;
import android.view.MenuItem;

import com.tp.Constants;
import com.tp.HubApplication;
import com.tp.MixpanelConstants;
import com.tp.R;
import com.tp.VariantConstants;
import com.tp.app.settings.ProfileActivity;
import com.tp.auth.Group;
import com.tp.auth.Session;
import com.tp.payment.PurchaseDialogFragment;
import com.tp.payment.UpgradeDialogFragment;
import com.tp.payment.UpgradePolicy;
import android.support.v4.app.DialogFragment;
import com.tp.payment.UpgradeDialogFragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.inject.Inject;

public class SettingsPreferenceActivity extends BasePreferenceActivity {
    private static final String MANAGE_HUBS_KEY = "MANAGE_HUBS";
    private static final String PROFILE_KEY = "PROFILE";
    private static final String UPGRADE_KEY = "UPGRADE";
    private static final String CALENDAR_KEY = "CALENDAR";
    private static final String FEEDBACK_KEY = "FEEDBACK";
    private static final String HELP_KEY = "HELP";
    private static final int INTENT_SEND_FEEDBACK_REQUEST = 23986;

    public static final String EXTRA_UPGRADE = "upgrade";

    @Inject
    Session mSession;

    @Inject
    UpgradePolicy mUpgradePolicy;

    private HubApplication mApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mApp = (HubApplication) getApplication();
        addPreferencesFromResource(R.xml.settings);

        findPreference(PROFILE_KEY).setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(SettingsPreferenceActivity.this, ProfileActivity.class);
                startActivity(intent);
                return true;
            }
        });

        if (mUpgradePolicy.shouldPromoteUpgrade()) {
            findPreference(UPGRADE_KEY).setOnPreferenceClickListener(new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
//                    Intent data = new Intent();
//                    data.putExtra(EXTRA_UPGRADE, true);
//                    setResult(RESULT_OK, data);
//                    finish();
                    DialogFragment dialogFragment =
                            UpgradeDialogFragment.dialogWithEntry("leftMenuUpgrade");
                    dialogFragment.show(getSupportFragmentManager(), EXTRA_UPGRADE);
                    return true;
                }
            });
        } else {
            findPreference(UPGRADE_KEY).setTitle("Upgraded");
        }

        findPreference(CALENDAR_KEY).setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(SettingsPreferenceActivity.this, CalendarSettingsActivity.class);
                startActivity(intent);
                return true;
            }
        });

        findPreference(FEEDBACK_KEY).setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                mApp.getMixPanel().track(MixpanelConstants.FEEDBACK_CLICKED, null);
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("message/rfc822");
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{VariantConstants.FEEDBACK_EMAIL});
                try {
                    startActivityForResult(intent, INTENT_SEND_FEEDBACK_REQUEST);
                } catch (ActivityNotFoundException e) {
                }
                return true;
            }
        });

        findPreference(HELP_KEY).setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(SettingsPreferenceActivity.this, HelpActivity.class);
                startActivity(intent);
                return true;
            }
        });

        getSupportActionBar().setTitle(R.string.settings_settings);
    }

    @Override
    protected void onResume() {
        super.onResume();

        PreferenceCategory hubCategory = (PreferenceCategory) findPreference(MANAGE_HUBS_KEY);
        hubCategory.removeAll();

        List<Group> allGroups = new ArrayList<Group>(mSession.getGroups());
        Collections.sort(allGroups, new Comparator<Group>() {
            @Override
            public int compare(Group lhs, Group rhs) {
                return lhs.getName().compareTo(rhs.getName());
            }
        });
        for (final Group group : allGroups) {
            Preference groupPref = new Preference(this);
            groupPref.setTitle(group.getName());
            groupPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference preference) {
                    Intent intent = new Intent(SettingsPreferenceActivity.this, GroupSettingsActivity.class);
                    intent.putExtra(Constants.ITEM_ID, group.getId());
                    startActivity(intent);
                    return true;
                }
            });
            hubCategory.addPreference(groupPref);
        }

        Preference createHubPref = new Preference(this);
        createHubPref.setTitle(R.string.settings_create_hub);
        createHubPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(SettingsPreferenceActivity.this, AddGroupActivity.class);
                startActivity(intent);
                return true;
            }
        });
        hubCategory.addPreference(createHubPref);
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                finish();
                return true;
            default:
                return super.onMenuItemSelected(featureId, item);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_SEND_FEEDBACK_REQUEST && resultCode == Activity.RESULT_OK) {
            mApp.getMixPanel().track(MixpanelConstants.FEEDBACK_SENT, null);
        }
    }
}

0 个答案:

没有答案