我创建了一个MainActivity
和一个SecondActivity
。当onClick
MainActivity
它启动时SecondActivity
。完成SecondActivity
后,它返回一个值,该值变成一个ImageView.setVisibility(View.VISIBLE);
并生成一个counter++
。
我的问题是,当我关闭应用程序然后再次重新启动它时,所有值都被清除了,我的ImageView
轮流ImageView.setVisibility(View.INVISIBLE);
又一次counter == 0
。
SecondActivity
即使我关闭应用程序或重新启动我的手机,我如何保存数据(结果)?我想public void onCreate(Bundle savedInstanceState) {
在每次重新启动应用程序时关闭应用程序之前进行设置。
我想要的是保存“int contadorliga”、“correcto1.setVisibility(View.VISIBLE);” 和“correcto2.setVisibility(View.VISIBLE);” 所以每次我重新启动应用程序时,它都是最近一次。这是我的代码:
package com.fakur.android.futbolquiz;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class Liga extends Activity {
/** Called when the activity is first created. */
static final int LIGA = 0;
int contadorbarcelona = 0;
int contadormadrid = 0;
int contadorliga = 0;
ImageView about;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liga);
TextView contador1 = (TextView) findViewById(R.id.contador1);
contador1.setText(contadorliga + "/20");
}
public void onBarcelonaClick(View botton) {
Intent intent = new Intent();
intent.setClass(this,Pregunta.class);
intent.putExtra("Barcelona", "Barcelona");
startActivityForResult(intent,LIGA);
}
public void onMadridClick(View botton) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(this,Pregunta.class));
intent.putExtra("Madrid", "Madrid");
startActivityForResult(intent,LIGA);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
TextView contador1 = (TextView) findViewById(R.id.contador1);
ImageView correcto1 = (ImageView) findViewById(R.id.correcto1);
ImageView correcto2 = (ImageView) findViewById(R.id.correcto2);
if (requestCode == LIGA){
if (resultCode == 1) {
if(contadorbarcelona == 0){
correcto1.setVisibility(View.VISIBLE);
contadorliga++ ;
contadorbarcelona++;
}
}
if (resultCode == 2) {
if(contadormadrid == 0){
correcto2.setVisibility(View.VISIBLE);
contadorliga++ ;
contadormadrid++;
}
}
}
contador1.setText(contadorliga + "/20");
}