我在保存应用程序某些部分的状态时遇到问题。
在布局中,我有 5 EditText
aButton
和 a TextView
。当用户输入值并计算(单击按钮)时,将TextView
填充 。
当方向改变时,没有任何反应,视图被传递并完美运行,但是当我(用我ViewPager
的)滑动时,填充的状态TextView
没有被保存。我有 3 个片段,如果我只滑动,比如从右到中,它会被保存,但是如果我向最左边滑动,TextView
当我返回片段时,它是未填充的,输入的值会EditText
被保存。
我的预期是onClick()
(正在进行计算并填充TextView
)的过程没有保存,我该如何处理?我曾尝试跟进Google 网站上的指南,但没有任何运气。
我的代码是:
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");
}
}