How do I save the status of an activity with sharedpreferences?

1

I want to know how I do to save the status of my activities, for example if I go in level 2, I mean in activity 2, as I do to save the textview that indicates the level, for example level2, and that when closing the app and open it again, when you click on the play button in the Mainactivity I load the activity 2 or the activity where it is left, or failing that if it is the first time I play and / or if it is in level 1 because when I click on play, load activity 1.

I want to do it with shared preferences.

Code of my activity1 called level 1:

public class Nivel1 extends Activity {



    public static Button r1,r2,r3,r4;
    static TextView npregunta;
    public static TextView preguntas,nivelactual;
    public static ImageView fotoperfil;
    public static Activity fa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nivel1);
        fa=this;
        preguntas=(TextView)findViewById(R.id.preguntas);
        nivelactual=(TextView)findViewById(R.id.nivelactual);
        fotoperfil=(ImageView)findViewById(R.id.fotoperfil);
        r1 = (Button) findViewById(R.id.respuesta1);
        r2 = (Button) findViewById(R.id.respuesta2);
        r3 = (Button) findViewById(R.id.respuesta3);
        r4 = (Button) findViewById(R.id.respuesta4);
        npregunta = (TextView) findViewById(R.id.npregunta);
        Preferencias.setLevel(getApplicationContext(), 1);
        r1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i4 = new Intent(Nivel1.this, popup.class);
                Intent i3 = new Intent(Nivel1.this, popupc.class);
                if (npregunta.getText().toString().equals("2")){
                startActivityForResult(i3,1);
                }

                else {

                    startActivity(i4);
                }
            }
        });

        r2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i4 = new Intent(Nivel1.this, popup.class);
                Intent i3 = new Intent(Nivel1.this, popupc.class);
                if (npregunta.getText().toString().equals("1")){
                    startActivityForResult(i3,1);
                }
                else {

                    startActivity(i4);
                }
            }
        });

        r3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i4 = new Intent(Nivel1.this, popup.class);
                Intent i3 = new Intent(Nivel1.this, popupc.class);
                if (npregunta.getText().toString().equals("3")
                       ){
                    startActivityForResult(i3,1);
                }

                else {

                    startActivity(i4);
                }
            }
        });

        r4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i4 = new Intent(Nivel1.this, popup.class);
                Intent i3 = new Intent(Nivel1.this, popupc.class);
                Intent i7 = new Intent(Nivel1.this, Transicion1.class);
                if (npregunta.getText().toString().equals("4") ){
                    startActivityForResult(i3,1);
                }
                else if(npregunta.getText().toString().equals("5")){

                    startActivity(i7);
                }
                else {

                    startActivity(i4);
                }
            }
        });

    }


@Override
    protected void onActivityResult(int peticion, int codigo, Intent data) {
       super.onActivityResult(peticion,codigo,data);
        if(peticion==1 && codigo==RESULT_OK)
{

            String resultado=data.getStringExtra("tpregunta");
            npregunta.setText(resultado);
            if(resultado.equals("2")){
                preguntas.setText(getText(R.string.pregunta2));
                r1.setText("respuesta21");
                r2.setText("respuesta22");
                r3.setText("respuesta23");
                r4.setText("respuesta24");}
            else if(resultado.equals("3")){
                preguntas.setText(getText(R.string.pregunta3));
                r1.setText("respuesta31");
                r2.setText("respuesta32");
                r3.setText("respuesta33");
                r4.setText("respuesta34");

            }
            else if(resultado.equals("4")){
                preguntas.setText(getText(R.string.pregunta4));
                r1.setText("respuesta41");
                r2.setText("respuesta42");
                r3.setText("respuesta43");
                r4.setText("respuesta44");

            }
            else if(resultado.equals("5")){
                preguntas.setText(getText(R.string.pregunta5));
                r1.setText("respuesta51");
                r2.setText("respuesta52");
                r3.setText("respuesta53");
                r4.setText("respuesta54");

            }


        }


    }

}

This is my activity 2:

public class Nivel2 extends Activity {

    private Nivel1 cx= new Nivel1();
    public static Button r1,r2,r3,r4;
    static TextView npregunta;
    public static TextView preguntas,nivelactual;
    public static ImageView fotoperfil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nivel2);
        cx.fa.finish();
        preguntas=(TextView)findViewById(R.id.preguntas);
        nivelactual=(TextView)findViewById(R.id.nivelactual);
        fotoperfil=(ImageView)findViewById(R.id.fotoperfil);
        r1 = (Button) findViewById(R.id.respuesta1);
        r2 = (Button) findViewById(R.id.respuesta2);
        r3 = (Button) findViewById(R.id.respuesta3);
        r4 = (Button) findViewById(R.id.respuesta4);
        npregunta = (TextView) findViewById(R.id.npregunta);
        Preferencias.setLevel(getApplicationContext(), 2 /* Nivel */);
        r1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i4 = new Intent(Nivel2.this, popup.class);
                Intent i3 = new Intent(Nivel2.this, popupn.class);
                if (npregunta.getText().toString().equals("7")){
                    startActivityForResult(i3,1);
                }

                else {

                    startActivity(i4);
                }
            }
        });

        r2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i4 = new Intent(Nivel2.this, popup.class);
                Intent i3 = new Intent(Nivel2.this, popupc.class);
                if (npregunta.getText().toString().equals("1")){
                    startActivityForResult(i3,1);
                }
                else {

                    startActivity(i4);
                }
            }
        });

        r3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i4 = new Intent(Nivel2.this, popup.class);
                Intent i3 = new Intent(Nivel2.this, popupn.class);
                if (npregunta.getText().toString().equals("6")
                        ){
                    startActivityForResult(i3,1);
                }

                else {

                    startActivity(i4);
                }
            }
        });

        r4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i4 = new Intent(Nivel2.this, popup.class);
                Intent i3 = new Intent(Nivel2.this, popupn.class);
                Intent i7=new Intent(Nivel2.this, popup.class);
                if (npregunta.getText().toString().equals("8") ){
                    startActivityForResult(i3,1);
                }
                else {

                    startActivity(i4);
                }
            }
        });
    }



    @Override
    protected void onActivityResult(int peticion, int codigo, Intent data) {
        super.onActivityResult(peticion, codigo, data);
        if (peticion == 1 && codigo == RESULT_OK) {

            String resultado=data.getStringExtra("tpregunta");
            npregunta.setText(resultado);

            if(resultado.equals("7")){
                preguntas.setText(getText(R.string.pregunta7));
                r1.setText("respuesta71");
                r2.setText("respuesta72");
                r3.setText("respuesta73");
                r4.setText("respuesta74");

            }
            else if(resultado.equals("8")){
                preguntas.setText(getText(R.string.pregunta8));
                r1.setText("respuesta81");
                r2.setText("respuesta82");
                r3.setText("respuesta83");
                r4.setText("respuesta84");

            }
        }


    }
}

and this is the code of my Mainactivity:

public class MainActivity extends AppCompatActivity {


    Button botoninfo,botonsonido,botonjugar;
    int contador,img,img2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sonidoboton = MediaPlayer.create(this, R.raw.teleport3);
        botoninfo = (Button) findViewById(R.id.informacion);
        botonsonido = (Button) findViewById(R.id.sonido);
        botonjugar= (Button)findViewById(R.id.comenzar);
        botoninfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                teleport.start();
                Intent i = new Intent(MainActivity.this, info.class);
                startActivity(i);
            }
        });


        botonjugar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                botonsonido.start();
                switch (Preferencias.getLevel(getApplicationContext())){

                    case 0:

                        break;
                    case 1:
                        Intent intentnivel1=new Intent(MainActivity.this, Nivel1.class);
                        startActivity(intentnivel1);
                        break;
                    case 2:
                        Intent intentnivel2=new Intent(MainActivity.this, Nivel2.class);
                        startActivity(intentnivel2);
                        break;
                    case 3:

                        break;
                }


            }
        });

    }
}
    
asked by Johan Berrio 25.05.2018 в 00:13
source

1 answer

1

I have something similar in an application, I have created a class to save and get the value of the "level":

import android.content.Context;
import android.content.SharedPreferences;

public class Preferencias {


    private static String PREFS_KEY = "mypreferences";

    public static void setLevel(Context context, int level) {
        SharedPreferences settings = context.getSharedPreferences(PREFS_KEY, context.MODE_PRIVATE);
        SharedPreferences.Editor editor;
        editor = settings.edit();
        editor.putInt("nivel", level);
        editor.commit();
    }



    public static int getLevel(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(PREFS_KEY, context.MODE_PRIVATE);
        return  preferences.getInt("nivel", 0);
    }


}

When you access MainActivity or your main activity and you get the preference value to determine the level:

    switch (Preferencias.getLevel(getApplicationContext())){

        case 0:
            //No realiza nada
            break;
        case 1:
            //Intent carga primer nivel
            break;
        case 2:
            //Intent carga segundo nivel
            break;
        case 3:
            //Intent carga tercer nivel
            break;
    }

for this in each Activity you can save the value of the level so that when you start the application determine which "level" to go to, for example if you are at level 1:

   Preferencias.setLevel(getApplicationContext(), 1 /* Nivel */);
    
answered by 25.05.2018 / 01:10
source