聆听偏好更改并将其应用到 android 工作室中的另一个 activity

Listening to preference changes and applying them on another activity in android studio

我的 MainActivity 中有一个 google 地图片段,还有一个 SettingsActivity 有一个 ListPreference 选项到 select 地图样式。我想做的是听取该偏好的变化,并相应地更改 MainActivity 上的地图。我只能在 onMapReady 上更改它,这是一次性的事情,它不是听众。我怎样才能做到这一点?我尝试在 SettingsActivity 中执行此操作,但无法访问 mapStyle

主要活动:

    private MapFragment mapFragment;
    String mapStyle;
    private GoogleMap map;
    private GoogleApiClient googleApiClient;
    SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        mapStyle = prefs.getString("list_preference_1", "<unset>");

        mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        if (googleApiClient == null) {
                    googleApiClient = new GoogleApiClient.Builder(this)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .addApi(LocationServices.API)
                            .build();
                }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        map.setOnMapClickListener(this);
        map.setOnMarkerClickListener(this);
        map.getUiSettings().setZoomControlsEnabled(true);

        try {
            // Customise the styling of the base map using a JSON object defined
            // in a raw resource file.
            int res;
            if (mapStyle.equals("Silver")){
                res = R.raw.silver_style;
            } else if (mapStyle.equals("Retro")) {
                res = R.raw.retro_style;
            } else if (mapStyle.equals("Dark")) {
                res = R.raw.dark_style;
            } else if (mapStyle.equals("Night")) {
                res = R.raw.night_style;
            } else if (mapStyle.equals("Aubergine")) {
                res = R.raw.aubergine_style;
            } else {
                res = R.raw.standard;
            }
            boolean success = googleMap.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(
                            this, res));

            if (!success) {
                Log.e(TAG, "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Can't find style. Error: ", e);
        }
    }

设置活动:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment())
                    .commit();
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }

也许不是最佳解决方案,但我认为您可以在 activity 简历上更新您的地图样式。打开设置 activity 并选择样式后,您可以 return 映射 activity。将调用 OnResume 函数,您将检查已选择的样式并根据此信息更新地图。

也许像这样就足够了:

@Override
protected void onResume() {
    super.onResume();
    SetMapStyle(map);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    map = googleMap;
    map.setOnMapClickListener(this);
    map.setOnMarkerClickListener(this);
    map.getUiSettings().setZoomControlsEnabled(true);
    SetMapStyle(googleMap);
}

private void SetMapStyle(GoogleMap googleMap){

    if(googleMap == null)
        return;

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    mapStyle = prefs.getString("list_preference_1", "<unset>");

    try {
        
        int res;
        if (mapStyle.equals("Silver")){
            res = R.raw.silver_style;
        } else if (mapStyle.equals("Retro")) {
            res = R.raw.retro_style;
        } else if (mapStyle.equals("Dark")) {
            res = R.raw.dark_style;
        } else if (mapStyle.equals("Night")) {
            res = R.raw.night_style;
        } else if (mapStyle.equals("Aubergine")) {
            res = R.raw.aubergine_style;
        } else {
            res = R.raw.standard;
        }
        boolean success = googleMap.setMapStyle(
                MapStyleOptions.loadRawResourceStyle(
                        this, res));

        if (!success) {
            Log.e(TAG, "Style parsing failed.");
        }
    } catch (Resources.NotFoundException e) {
        Log.e(TAG, "Can't find style. Error: ", e);
    }

}