我有一个列表视图活动,它打开附加到其列表项的不同活动。每个活动都有多个微调器和一个提交按钮,在按下时会在文本中显示相应活动的视图。然后再按下我们再次进入列表查看活动并通过单击其中一个列表项打开其他一些活动。现在当我回到之前打开的前一个活动之前我在文本View中得到结果时,它只是清除数据并再次启动。所有微调器状态和文本视图结果都被清除。
我想恢复我之前离开的所有活动。我听说过活动生命周期,但不知道放在哪里的方法。 请帮忙
据我所知这是我的代码
public class CannonsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cannon_spinner_numbers);
sbmt=(Button)findViewById(R.id.submit);
tx=(TextView)findViewById(R.id.upgradeResult);
sp1=(Spinner)findViewById(R.id.spinner);
sp2=(Spinner)findViewById(R.id.spinner1);
sp3=(Spinner)findViewById(R.id.spinner2);
sp4=(Spinner)findViewById(R.id.spinner3);
sp5=(Spinner)findViewById(R.id.spinner4);
sp6=(Spinner)findViewById(R.id.spinner5);
sp7=(Spinner)findViewById(R.id.spinner6);
SpinnerAdapter adapter=new SpinnerAdapter(this,levels,images);
sp1.setAdapter(adapter);
sp2.setAdapter(adapter);
sp3.setAdapter(adapter);
sp4.setAdapter(adapter);
sp5.setAdapter(adapter);
sp6.setAdapter(adapter);
sp7.setAdapter(adapter);
final Bundle bundle = getIntent().getExtras();
if (bundle.getString("classFrom").equals(TownHallNineActivity.class.toString())) {
sp6.setVisibility(View.GONE);
sp7.setVisibility(View.GONE);
}
if (bundle.getString("classFrom").equals(TownHallTenActivity.class.toString())) {
sp7.setVisibility(View.GONE);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++??
if (bundle.getString("classFrom").equals(TownHallTenActivity.class.toString())) {
cannonLevelFourteen=0;
}
if(bundle.getString("classFrom").equals(TownHallNineActivity.class.toString())){
cannonLevelFourteen=0;
cannonLevelThirteen=0;
cannonLevelTwelve=0;
}
if(bundle.getString("classFrom").equals(TownHallElevenActivity.class.toString())){
}
//++++++++++++++++++++++++++++++++++++++++++++//
//Spinner 1
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences sharedPreferences = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
switch (position){
case 0:
sp1CannonUpgradeCost = (cannonLevelTwo + cannonLevelThree + cannonLevelFour + cannonLevelFive +
cannonLevelSix + cannonLevelSeven + cannonLevelEight + cannonLevelNine + cannonLevelTen +
cannonLevelEleven + cannonLevelTwelve + cannonLevelThirteen + cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 1:
sp1CannonUpgradeCost= (cannonLevelThree+cannonLevelFour+cannonLevelFive+
cannonLevelSix+cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 2:
sp1CannonUpgradeCost= (cannonLevelFour+cannonLevelFive+
cannonLevelSix+cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 3:
sp1CannonUpgradeCost= (cannonLevelFive+
cannonLevelSix+cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen)+cannonLevelFourteen;
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 4:
sp1CannonUpgradeCost= (
cannonLevelSix+cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 5:
sp1CannonUpgradeCost= (cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 6:
sp1CannonUpgradeCost= (cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 7:
sp1CannonUpgradeCost= (cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 8:
sp1CannonUpgradeCost= (cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 9:
sp1CannonUpgradeCost= (
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 10:
sp1CannonUpgradeCost= (cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 11:
sp1CannonUpgradeCost= (cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 12:
sp1CannonUpgradeCost= (cannonLevelFourteen);
editor.putInt("sp1_cannon", sp1CannonUpgradeCost);
editor.apply();
break;
case 13:
sp1CannonUpgradeCost = (0);
editor.putInt("sp1_cannon",sp1CannonUpgradeCost);
editor.apply();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences sharedPreferences = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
switch (position){
case 0:
sp2CannonUpgradeCost = (cannonLevelTwo + cannonLevelThree + cannonLevelFour + cannonLevelFive +
cannonLevelSix + cannonLevelSeven + cannonLevelEight + cannonLevelNine + cannonLevelTen +
cannonLevelEleven + cannonLevelTwelve + cannonLevelThirteen + cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 1:
sp2CannonUpgradeCost= (cannonLevelThree+cannonLevelFour+cannonLevelFive+
cannonLevelSix+cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 2:
sp2CannonUpgradeCost= (cannonLevelFour+cannonLevelFive+
cannonLevelSix+cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 3:
sp2CannonUpgradeCost= (cannonLevelFive+
cannonLevelSix+cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen)+cannonLevelFourteen;
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 4:
sp2CannonUpgradeCost= (
cannonLevelSix+cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 5:
sp2CannonUpgradeCost= (cannonLevelSeven+cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 6:
sp2CannonUpgradeCost= (cannonLevelEight+cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 7:
sp2CannonUpgradeCost= (cannonLevelNine+cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 8:
sp2CannonUpgradeCost= (cannonLevelTen+
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 9:
sp2CannonUpgradeCost= (
cannonLevelEleven+cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 10:
sp2CannonUpgradeCost= (cannonLevelTwelve+cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 11:
sp2CannonUpgradeCost= (cannonLevelThirteen+cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 12:
sp2CannonUpgradeCost= (cannonLevelFourteen);
editor.putInt("sp2_cannon", sp2CannonUpgradeCost);
editor.apply();
break;
case 13:
sp2CannonUpgradeCost = (0);
editor.putInt("sp2_cannon",sp2CannonUpgradeCost);
editor.apply();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
对于另外4个Spinners来说,这是相同的
这是显示结果的末尾的代码
sbmt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences shrd = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = shrd.edit();
int sp1cannonUpgradeCostRequired = shrd.getInt("sp1_cannon", -1);
int sp2cannonUpgradeCostRequired = shrd.getInt("sp2_cannon", -1);
int sp3cannonUpgradeCostRequired = shrd.getInt("sp3_cannon", -1);
int sp4cannonUpgradeCostRequired = shrd.getInt("sp4_cannon", -1);
int sp5cannonUpgradeCostRequired = shrd.getInt("sp5_cannon", -1);
int sp6cannonUpgradeCostRequired = shrd.getInt("sp6_cannon", -1);
int sp7cannonUpgradeCostRequired = shrd.getInt("sp7_cannon", -1);
Bundle bundle = getIntent().getExtras();
if (bundle.getString("classFrom").equals(TownHallNineActivity.class.toString())) {
totalCannonTh8UpgradeCost=sp1cannonUpgradeCostRequired+sp2cannonUpgradeCostRequired+
sp3cannonUpgradeCostRequired+sp4cannonUpgradeCostRequired+sp5cannonUpgradeCostRequired
;
}
if (bundle.getString("classFrom").equals(TownHallTenActivity.class.toString())) {
totalCannonTh8UpgradeCost=sp1cannonUpgradeCostRequired+sp2cannonUpgradeCostRequired+
sp3cannonUpgradeCostRequired+sp4cannonUpgradeCostRequired+sp5cannonUpgradeCostRequired+sp6cannonUpgradeCostRequired
;
}
if (bundle.getString("classFrom").equals(TownHallElevenActivity.class.toString())) {
totalCannonTh8UpgradeCost=sp1cannonUpgradeCostRequired+sp2cannonUpgradeCostRequired+
sp3cannonUpgradeCostRequired+sp4cannonUpgradeCostRequired+sp5cannonUpgradeCostRequired+sp6cannonUpgradeCostRequired
+sp7cannonUpgradeCostRequired
;
}
editor.putLong("CannonTh8FinalUpgradeCostRequired", totalCannonTh8UpgradeCost);
tx.setText(""+totalCannonTh8UpgradeCost);
Toast.makeText(CannonsActivity.this, "done", Toast.LENGTH_SHORT).show();
}
});
}