0

This is the code for an Activity named as Interest Activity. It is a dashboard which consist of four interest/hobbies (gaming, singing, sports, and coding). The user is asked to choose one of the them and then he/she is redirected to the particular activity. I used Cardview to represent the interest they behave like a button. This is the code:

package com.example.meetup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class InterestActivity extends AppCompatActivity {

    CardView cardGame;
    CardView cardSports;
    CardView cardSing;
    CardView cardCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest);

        cardGame = findViewById(R.id.cardGame);
        cardCode = findViewById(R.id.cardCode);
        cardSing = findViewById(R.id.cardSing);
        cardSports = findViewById(R.id.cardSports);

        cardGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(InterestActivity.this, "Gaming", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(InterestActivity.this,GameActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }
        });

        cardSports.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(InterestActivity.this, "Sports", Toast.LENGTH_SHORT).show();
            }
        });

        cardSing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(InterestActivity.this, "Singing", Toast.LENGTH_SHORT).show();
            }
        });

        cardCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(InterestActivity.this, "Coding", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Now the problem is that whenever I am reopening the app, it takes me to the interest activity again even though I chose an interest once. Then I got to know about shared preference as how it stores the user data and intent accordingly.

I saw many tutorials on YouTube as to how could I use it, but they all were taking something as input like text. But in my case I’m not taking any input just pushing a button. How do I use a shared preference in my case? Can you give a shared preference code for if the user chooses gaming?

Akshat kant
  • 125
  • 6

2 Answers2

1

If you want to store what the user clicked the last time and do something about it, you can follow this to write/read from shared preferences: https://stackoverflow.com/a/3624358/7365491

Every time the user clicks on one of your CardViews you store a value of the last selected interest.

Every time the user opens the app in the onCreate, check if the preference exists and read the last stored value in it. With that then you can decide what you want to do when the app is opened and the user has previously selected a value.

1

Define some constants to manage saved selected interest

public class InterestActivity extends AppCompatActivity {

    // Define a string key for SharedPreference value
    private static final String SP_INTEREST = "interest"

    // Define integer codes for each interest
    private static final int INTEREST_NONE = 0
    private static final int INTEREST_GAME = 1
    private static final int INTEREST_SPORTS = 2
    private static final int INTEREST_SING = 3
    private static final int INTEREST_CODE = 4

    SharedPreferences sp;
    SharedPreferences.Editor editor;

    CardView cardGame;
    CardView cardSports;
    CardView cardSing;
    CardView cardCode;

    // Rest of the code

}

Then for saving the choosen interest, repeat it for each on click:

    cardGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(InterestActivity.this, "Gaming", Toast.LENGTH_SHORT).show();

            // Here first save the choosen interest
            editor.putInt(SP_INTEREST, INTEREST_GAME);
            editor.apply();

            Intent intent = new Intent(InterestActivity.this,GameActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        }
    });

And when you restart the app, in on create method check the saved interest code and start the corresponding activity.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get and init SharedPreferences and editor
        SharedPreferences sp = getSharedPreferences(getPackageName(), MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        // INTEREST_NONE is the default value if there no registry.
        int savedInterest = sp.getInt(SP_INTEREST, INTEREST_NONE);

        if(INTEREST_GAME == savedInterest) {
            // Start game activity using intent
            Intent intent = new Intent(InterestActivity.this,GameActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        }
        else if(INTEREST_SPORTS == savedInterest) {
            // Start sports activity using intent
        }
        else if(INTEREST_SING == savedInterest) {
            // Start sing activity using intent
        }
        else if(INTEREST_CODE == savedInterest) {
            // Start code activity using intent
        }

        setContentView(R.layout.activity_interest);
        // Rest of the onCreate codes
    }
Kozmotronik
  • 2,080
  • 3
  • 10
  • 25