我有一个保存应用程序某些部分状态的突变。保存状态只保存部分片段
在布局中我有5 EditText
a Button
和TextView
。当用户输入数值并计算(按钮单击)时,将填充TextView
。
当方向改变什么都没有发生时,视图被传递并完美运行,但是当我滑动(使用我的ViewPager
)时,没有保存填充的TextView
的状态。我有3个片段,如果我只刷卡,说从右到中间它被保存,但是如果我滑动到最左边TextView
是未填充的,当我回到片段,EditText
中输入的值被保存。
我期待的是,从onClick()
过程(这是做计算和填充TextView
)不被保存,我怎么用这个去了?我试图跟上Googles site的指南,没有任何运气。
我的代码有云:
public class FuelConsumptionFragment extends Fragment implements OnClickListener {
View view;
int savedState = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
savedState = savedInstanceState.getInt("curChoice", 0);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", savedState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fuel_consumption, container, false);
Button calculate_fuel = (Button)view.findViewById(R.id.calculate_fuel);
calculate_fuel.setOnClickListener(this);
return view;
}
清单:
<activity
android:name="simcas.fartberegneren.MainActivity"
android:icon="@drawable/fartberegneren"
android:label=""
android:configChanges="orientation|screenSize" >
</activity>
编辑:我有一个模糊的概念,在onCreateView
通货膨胀可能是问题,这是正确的?
编辑2:在我的代码只是去,实现我的“定制”适配器可能是问题的一部分,该代码有云:
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new SpeedZonesFragment();
case 1: return new DistanceFragment();
case 2: return new FuelConsumptionFragment();
default: return null;
}
}
}
莫非这导致的问题?正如我看到我正在创建新的Fragment
切换位置时,是正确的吗?
编辑3:代码为onClick()
:
@Override
public void onClick(View view) {
DecimalFormat format = new DecimalFormat("#.#");
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
EditText distance_entry_fuel = (EditText) getActivity().findViewById(R.id.distance_fuel);
EditText speed_a_entry_fuel = (EditText) getActivity().findViewById(R.id.speed_a_fuel);
EditText gas_a_use = (EditText) getActivity().findViewById(R.id.gas_use_a);
EditText speed_b_entry_fuel = (EditText) getActivity().findViewById(R.id.speed_b_fuel);
EditText gas_b_use = (EditText) getActivity().findViewById(R.id.gas_use_b);
EditText gas_price = (EditText) getActivity().findViewById(R.id.gas_price);
distance_entry_fuel.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
speed_a_entry_fuel.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
gas_a_use.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
speed_b_entry_fuel.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
gas_b_use.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
gas_price.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
distance_entry_fuel.clearFocus();
speed_a_entry_fuel.clearFocus();
gas_a_use.clearFocus();
speed_b_entry_fuel.clearFocus();
gas_b_use.clearFocus();
double moneySpend;
double timeArest;
double timeArest1;
double timeBrest;
double timeBrest1;
double totalTime;
double totalTimerest;
try {
double distance = Double.parseDouble(distance_entry_fuel.getText().toString());
double speedA = Double.parseDouble(speed_a_entry_fuel.getText().toString());
double gasA = Double.parseDouble(gas_a_use.getText().toString());
double speedB = Double.parseDouble(speed_b_entry_fuel.getText().toString());
double gasB = Double.parseDouble(gas_b_use.getText().toString());
double gasPrice = Double.parseDouble(gas_price.getText().toString());
double gasUseA = (distance/gasA);
double gasUseB = (distance/gasB);
if (speedB > speedA) {
if (gasUseB > gasUseA) {
moneySpend = ((gasUseB - gasUseA) * gasPrice);
}
else {
moneySpend = ((gasUseA - gasUseB) * gasPrice);
}
timeArest = (distance % speedA); // Calculate the remainder of A
timeArest1 = ((timeArest/speedA) * 60); // Convert the remainder of A to minutes
timeBrest = (distance % speedB); // Calculate the remainder of B
timeBrest1 = ((timeBrest/speedB) * 60); // Convert the remainder of B to minutes
totalTime = (int)((distance/speedA) - (distance/speedB)); // Calculate the amount of hours saved
if (timeArest >= timeBrest) { // Condition for calculating time
totalTimerest = (timeArest1 - timeBrest1);
}
else { // opposite condition
totalTimerest = ((timeArest1 - timeBrest1) + 60); // Make up for the negative number
}
}
else {
if (gasUseB > gasUseA) {
moneySpend = ((gasUseB - gasUseA) * gasPrice);
}
else {
moneySpend = ((gasUseA - gasUseB) * gasPrice);
}
timeArest = (distance % speedA); // Calculate the remainder of A
timeArest1 = ((timeArest/speedA) * 60); // Convert the remainder of A to minutes
timeBrest = (distance % speedB); // Calculate the remainder of B
timeBrest1 = ((timeBrest/speedB) * 60); // Convert the remainder of B to minutes
totalTime = (int)((distance/speedB) - (distance/speedA)); // Calculate the amount of hours saved
if (timeBrest >= timeArest) { // Condition for calculating time
totalTimerest = (timeBrest1 - timeArest1);
}
else { // opposite condition
totalTimerest = ((timeBrest1 - timeArest1) + 60); // Make up for the negative number
}
}
String minuteTotal = "";
String hourTotal = "";
if (totalTimerest == 1) {
minuteTotal = " minut";
}
else {
minuteTotal = " minutter";
}
if (totalTime == 1) {
hourTotal = " time";
}
else {
hourTotal = " timer";
}
String moneySpendOnGas;
String distanceText = "Over en strækning på " + format.format(Double.parseDouble(distance_entry_fuel.getText().toString())) + " km";
String gasUsedA = "Bruger du " + format.format(gasUseA) + " liter benzin, ved en gennemsnitsfart på " + speedA + " km/t.";
String gasUsedB = "Bruger du " + format.format(gasUseB) + " liter benzin, ved en gennemsnitsfart på " + speedB + " km/t.";
if (totalTime > 0) {
if (totalTimerest > 0) {
moneySpendOnGas = "Det koster dig " + format.format(moneySpend) + " kr at komme " + (int)totalTime + hourTotal + " og " + (int)totalTimerest + minuteTotal + " hurtigere frem.";
}
else {
moneySpendOnGas = "Det koster dig " + format.format(moneySpend) + " kr at komme " + (int)totalTime + hourTotal + " hurtigere frem.";
}
}
else {
moneySpendOnGas = "Det koster dig " + format.format(moneySpend) + " kr at komme " + (int)totalTimerest + minuteTotal + " hurtigere frem.";
}
TextView distance_text_fuel = (TextView) getActivity().findViewById(R.id.distance_text_fuel);
distance_text_fuel.setText(distanceText);
TextView gas_used_a = (TextView) getActivity().findViewById(R.id.gas_used_a);
gas_used_a.setText(gasUsedA);
TextView gas_used_b = (TextView) getActivity().findViewById(R.id.gas_used_b);
gas_used_b.setText(gasUsedB);
TextView money_spend = (TextView) getActivity().findViewById(R.id.money_spend);
money_spend.setText(moneySpendOnGas);
}
catch (NumberFormatException e) {
DialogFragment alert = new EntryAlertDialog();
alert.show(getFragmentManager(), "Alert");
}
}
尝试调用'ViewPager.setOffscreenPageLimit(2)' – 2013-02-25 13:27:41
@vmironov:虽然这实际上工作,它并没有真正解决保存状态的问题,所以如果fx我有10个片段在viewpager它会使用巨大的金额的记忆权利? – Evilunclebill 2013-02-25 13:31:38
是的。你必须实现'onSaveInstanceState',然后正确地恢复片段状态 – 2013-02-25 13:37:47