i have created a MainActivity and a SecondActivity. When onClick MainActivity it starts the SecondActivity. When SecondActivity finish, it returns a value that turns an ImageView.setVisibility(View.VISIBLE); and makes a counter++.
My problem is that when i close the application and then restart it again all the values were cleared and my ImageView turns ImageView.setVisibility(View.INVISIBLE); and the counter == 0 again.
How can i save data (the result of SecondActivity) even if i close the aplication or reboot my mobile? I want to set public void onCreate(Bundle savedInstanceState) { as before i close the application each time i restart it.
What I want is to save “int contadorliga”, “correcto1.setVisibility(View.VISIBLE);” and “correcto2.setVisibility(View.VISIBLE);” so each time I restar the app it is as the latest time it was. This is my code:
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");
}
Usage of shared preferences is best fit for you, see here
Edit:
Added source below in response to your comment;
I hope this should work for you. Good Luck!