刷新片段从设置更改背面查看

时间:2013-05-09 18:54:55

标签: android android-fragments android-nested-fragment

我想要完成的是当有人从设置中返回并更改其偏好时,我的视图会刷新。我认为这会奏效,但事实并非如此。关于如何实现这一目标的任何想法?

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import com.projectcaruso.naturalfamilyplanning.R;
    import android.annotation.SuppressLint;
    import android.app.DatePickerDialog.OnDateSetListener;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.DatePicker;
    import android.widget.TextView;

    public class ChartingFragment extends Fragment implements OnDateSetListener {
        SharedPreferences mPreferences;
        Boolean symptothermal;
        Boolean mucus_stamps;
        Boolean fertile_infertile;

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // TODO Auto-generated method stub
        }    

        public void showDatePickerDialog(View v) {
            DatePickerFragment newFragment = new DatePickerFragment();
            newFragment.show(getChildFragmentManager(), "datePicker");
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            init();
        }           

        private void init() {
            mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
            symptothermal = mPreferences.getBoolean("symptothermal", true);
            mucus_stamps = mPreferences.getBoolean("mucus_stamps", true);
            fertile_infertile = mPreferences.getBoolean("fertile_infertil", true);
        }

        @SuppressLint("SimpleDateFormat")
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view;

            view = setcontrolvisability(inflater);

            return view;
           }

        @Override
        public void onResume() { 
              super.onResume(); 
              View vg = getView().findViewById(R.id.charting);
              vg.invalidate();
              init();

        }

        private View setcontrolvisability(LayoutInflater inflater) {
            // TODO Auto-generated method stub
            View view = inflater.inflate(R.layout.fragment_charting, null);
            TextView temp;
            if (!symptothermal) {
                    temp = (TextView) view.findViewById(R.id.temp);
                    temp.setVisibility(View.GONE);
                    temp = (TextView) view.findViewById(R.id.tempvalue);
                    temp.setVisibility(View.GONE);
                }

            if (!mucus_stamps) {
                temp = (TextView) view.findViewById(R.id.stamp);
                temp.setVisibility(View.GONE);
                temp = (TextView) view.findViewById(R.id.stampvalue);
                temp.setVisibility(View.GONE);
            }

            if (!fertile_infertile) {
                temp = (TextView) view.findViewById(R.id.fertile);
                temp.setVisibility(View.GONE);
                temp = (TextView) view.findViewById(R.id.fertileswitch);
                temp.setVisibility(View.GONE);
            }

            temp  = (TextView) view.findViewById(R.id.dateselected);
            SimpleDateFormat dfDate_day= new SimpleDateFormat("MM/dd/yyyy");
            String dt="";
            Calendar c = Calendar.getInstance(); 
            dt=dfDate_day.format(c.getTime());
            temp.setText(dt);

            return view;
        }


    }

1 个答案:

答案 0 :(得分:0)

如果子活动(FragmentActivity)已经更改,我建议使用startActivityForResult并传递Bundle。

1)从您的活动开始首选项活动:

Intent intent = new Intent(this, PreferenceActivity.class);
if (!this.isFinishing())
startActivityForResult(intent, REQUEST_UPDATE);

2)覆盖onActivityResult

@Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
    super.onActivityResult(reqCode, resCode, data);
    Log.d(TAG, "Received Result: " + reqCode + " / " + resCode);
}

3)在Subactivity(PreferenceActivity)中监视哪些键已被更改,如果你愿意,可以将它作为一个包传递

public class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener,
    OnPreferenceChangeListener {

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

       // Load the preferences from an XML resource
       addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public boolean onPreferenceChange(Preference arg0, Object arg1) {
        getParent().setResult(MainActivity.RESULT_PREFERENCES_CHANGED); // constant
    }

@Override
public void onResume() {
    super.onResume();
    getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    super.onPause();
}
}