简体   繁体   中英

How to use Shared Preferences in class to set all activities?

First of all: I searched for my question in StackOverflow.

Android - Using Shared Preferences in separate class?

And I get null expectation.

I am making a simple 2D game for Android Platform. I have to set only one level value for making this game. For making my game; I have created 3 activities. First activity; Takes the level number, and pass to the second one ( playground ). After that; İf gaming is passed, The second activity pass to the third. And When the user clicks to button level will be up ( +1 ). Level number is always 1 or 1 more than. I used to Shared Preference for saving my value but I does not work. How can I do it?

Here is my codes :

public class shared_level {
   int level = 1;

    private static Context context;



    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public final static String PREFS_NAME = "Level_preference_name";


    public static void setInt( String key, int level) {
        SharedPreferences sharedPref = context.getSharedPreferences(PREFS_NAME,0);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putInt(key, level);
        editor.apply();
    }

    public static int getInt(String key) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        return prefs.getInt(key, 0 );
    }


}

First actvitiy :

 final shared_level shared_level =new shared_level();

    Toast.makeText(this, "" + shared_level.getLevel(), Toast.LENGTH_SHORT).show();




    pass_to_second.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent git = new Intent(getApplicationContext(), second.class);
            git.putExtra("level" ,String.valueOf(shared_level.getLevel())  );
            startActivity(git);
            MainActivity.this.finish();



        }
    });
}

Second activity :

Bundle extras = getIntent().getExtras();
    final String value = extras.getString("level");

    Toast.makeText(this, "Level is : " + "   " + value, Toast.LENGTH_SHORT).show();



    sayı.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent git = new Intent(getApplicationContext(), third.class);
            git.putExtra("level", String.valueOf(value));
            startActivity(git);

            second.this.finish();
        }
    });

Third's button activity goes to first :

Bundle extras = getIntent().getExtras();
    final String value = extras.getString("level");


   final shared_level shared_level =new shared_level();

    int new_level =Integer.valueOf(value) + 1;

  shared_level.setLevel(new_level);

 Toast.makeText(this, "" + shared_level.getLevel(), Toast.LENGTH_SHORT).show();


    sayım.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Intent i = new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);
            third.this.finish();





        }
    });

You should pass context to shared_level class via the constructor.

public class shared_level {
    private Context context;

    public shared_level(Context context) {
            this.context = context;
        }
    ...
}

and create the instance of shared_level class like:

final shared_level shared_level = new shared_level(yourActivity.this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM