0

I tried to do this

@Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPreferences.Editor editor=this.getMyView().getSp().edit();
        editor.putInt("score",this.getMyView().getScores()+this.getMyView().getScore());
        editor.commit();

and it doesn't work

can someone pls help me?

its MyView class the class is working the problem isn't there and it is not the entire class case it is too long

package com.example.brickv5;
import...
public class MyView extends View {
    private ArrayList<Brick> brickList = new ArrayList<Brick>();
    private LocalTime time;
    private Brick brick;
    private Killer killer;
    private Bitmap back;
    private int score=0;
    private Timer timer;
    private SharedPreferences sp= getContext().getSharedPreferences("scores",0);
    public SharedPreferences getSp() {
        return sp;
    }
    private int scores;
    private int width=Resources.getSystem().getDisplayMetrics().widthPixels;
    private int height=Resources.getSystem().getDisplayMetrics().heightPixels;
    public MyView(Context context) {
        super(context);
        for (int i = 0; width-(width-20)/5*(i+1)>=0; i++) {
            int x = width-(width-20)/5*(i+1);
            int y = (300);
            brick = new Brick(x, y, this);
            brickList.add(brick);
        }
        back = BitmapFactory.decodeResource ( this.getResources (), R.drawable.background2);
        back=Bitmap.createScaledBitmap(back,width,height,false);
        killer = new Killer(width/2, height-height/3, 0, 0, this);
        killer.setX(killer.getX()-killer.getBitmap().getWidth()/2);
        scores=sp.getInt("score",0);
        timer=new Timer(width,100);
    }
  • what's `getMyView().getSp()`? and how do you read the values? – ATP Jan 24 '22 at 13:30
  • MyView -> class sp-> folder getMyView().getSp() -> the way to get the folder i where i save the score – yosef vaksin Jan 24 '22 at 13:36
  • Can you edit your post and add this function? and how you read the information? – ATP Jan 24 '22 at 13:38
  • as wrote in the answer, use `onPause()` and not `onDestroy()` https://stackoverflow.com/questions/27681366/difference-between-saving-sharedpreferences-in-onpause-and-ondestroy-methods – ATP Jan 24 '22 at 14:27

0 Answers0