ビューページャーを使用してフラグメントをスクロールしています。設定変更時にフラグメントを非表示にしたい
package app.converto;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import app.converto.settings.SettingsActivity;
import app.converto.ui.LengthFragment;
import app.converto.ui.TemperatureFragment;
public class ConverTo extends FragmentActivity /*implements OnClickListener*/ implements OnSharedPreferenceChangeListener {
SectionsPagerAdapter mSectionsPagerAdapter; //declaring pageradapter class
ViewPager mViewPager; // declaring viewpager class
private TemperatureFragment fragment2;
private LengthFragment fragment1;
private static final int RESULT_SETTINGS = 1;
@Override
public void onCreate(Bundle savedInstanceState) { //methods to call on app launch
super.onCreate(savedInstanceState);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
setContentView(R.layout.converto_home);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
sharedPref.registerOnSharedPreferenceChangeListener(this);
//boolean prefTemperature = sharedPref.getBoolean("prefTemperature", true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent i = new Intent(this, SettingsActivity.class);
startActivityForResult(i, RESULT_SETTINGS);
break;
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SETTINGS:
showUserSettings();
break;
}
}
private void showUserSettings() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
StringBuilder builder = new StringBuilder();
builder.append(
sharedPrefs.getBoolean("pref_temperature", true));
builder.append(
sharedPrefs.getBoolean("pref_length", false));
builder.append(
sharedPrefs.getBoolean("pref_area", false));
// TextView settingsTextView = (TextView) findViewById(R.id.textUserSettings);
// settingsTextView.setText(builder.toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch(i){
case 0:
fragment2 = new TemperatureFragment(ConverTo.this);
return fragment2;
case 1:
fragment1 = new LengthFragment(ConverTo.this);
return fragment1;
}
return null;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.heading_temperature);
case 1: return getString(R.string.heading_length);
}
return null;
}
}
public void onSharedPreferenceChanged(SharedPreferences arg0, String key) {
if (key.equals("prefTemperature")) {
Toast.makeText(this, "here!", Toast.LENGTH_LONG).show();
FragmentManager fM = getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
fT.hide(fragment1);
fT.commit();
}
}
}
これは、04-07 21:30:14.313: E/AndroidRuntime(15363): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState というエラーです。
これは私の温度フラグメントコードです
package app.converto.ui;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import app.converto.R;
import app.converto.utils.Temperature;
@SuppressLint("ValidFragment")
public class TemperatureFragment extends Fragment implements OnClickListener, OnItemSelectedListener {
EditText rInputTemp;
public static String rSelectedUnit;
Spinner spinner;
List<String> rValues = new ArrayList<String>();
Temperature rTemperature;
Context rContext;
ListView rResultsList;
public TemperatureFragment(Context context) {
rContext = context;
}
public TemperatureFragment(){
}
public Context getContext(){
return rContext;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.view_temperature, container, false);
rInputTemp = (EditText) view.findViewById(R.id.inputTemp);
rInputTemp.setText("1");
rInputTemp.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
if(s.toString().equals("")){
}
else{
rTemperature = new Temperature(spinner.getSelectedItemPosition(), Double.parseDouble(s.toString()));
setDisplayText(rTemperature.getValues());
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
rResultsList = (ListView) view.findViewById(R.id.results);
spinner = (Spinner) view.findViewById(R.id.temp_units);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getApplicationContext(), R.array.units_temperature,R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
adapter.setDropDownViewResource(R.layout.spinner_layout);
return view;
}// end oncreate
public void onClick(View v) {
}
public void onItemSelected(AdapterView<?> p, View arg1, int pos,
long id) {
rSelectedUnit = p.getItemAtPosition(pos).toString();
rTemperature = new Temperature(spinner.getSelectedItemPosition(), Double.parseDouble(rInputTemp.getText().toString()));
setDisplayText(rTemperature.getValues());
}//spinneronItemSelected
public void onNothingSelected(AdapterView<?> p) {
}//spinner onNothingSelected
double roundfourDecimals(double d) {
DecimalFormat fourDForm = new DecimalFormat("#.####");
return Double.valueOf(fourDForm.format(d));
}
private void setDisplayText(List<String> values){
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(rContext, R.layout.result,R.id.result,values);
rResultsList.setAdapter(adapter2);
}
}//end fragment 2