1

I am new to android so in the interest of learning i made myself a simple game where the user finds an image in one activity "scene1" then once they press the image it moves to the next activity "scene2", All works fine. What i want to do is save what level the user gets to so they can continue the next time they start the game by clicking a load save data button for example.

I have looked into shared prefs and writing to a file in the internal storage but i can not seem to get my head around it :( It would be really helpful if you could offer me some help and get me moving in the right direction :)

Below is the code i use for each level.

public class scene1 extends Activity implements OnClickListener {

private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
private final long startTime = 20 * 1000;
private final long interval = 1 * 1000;

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle("FindIt")
    .setMessage("Exit to main menu?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();    
    }

})
.setNegativeButton("No", null)
.show();
}

ImageButton imageButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    startB = (Button) this.findViewById(R.id.button);
    startB.setOnClickListener(this);
    text = (TextView) this.findViewById(R.id.timer);
    countDownTimer = new MyCountDownTimer(startTime, interval);
    text.setText(text.getText() + String.valueOf(startTime/1000));
    }

    @Override
    public void onClick(View v) {
    startB.setVisibility(View.INVISIBLE);   
    if (!timerHasStarted) {
    countDownTimer.start();
    timerHasStarted = true;
    startB.setText("STOP");
    } else {
    countDownTimer.cancel();
    timerHasStarted = false;
    startB.setText("RESTART");
    }
    }


    public class MyCountDownTimer extends CountDownTimer {
    public MyCountDownTimer(long startTime, long interval) {
    super(startTime, interval);
    }

    @Override
    public void onFinish() {
        Intent intent = new Intent(scene1.this, timeUp.class);
        scene1.this.startActivity(intent);
        finish();
            }

    @Override
    public void onTick(long millisUntilFinished) {
    text.setText("" + millisUntilFinished/1000);


    addListenerOnButton();

}

public void addListenerOnButton() {

    imageButton = (ImageButton) findViewById(R.id.it);

    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

           Toast.makeText(scene1.this,
            "You found It!", Toast.LENGTH_LONG).show();

           Intent intent = new Intent(scene1.this, scene2.class);
           scene1.this.startActivity(intent);
           android.os.Process.killProcess(android.os.Process.myPid());
           finish();

        }

    }); 
}

}
}
Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
  • 3
    Oh good grief... `android.os.Process.killProcess(android.os.Process.myPid());` DO NOT do that EVER!!! Using `finish()` is a graceful way to end an `Activity` don't EVER kill your process like that. – Squonk Jul 02 '13 at 19:15

2 Answers2

0

You mentioned the solution yourself: Use shared preferences. Read the preferences in onCreate() and write them in the onDestroy()

This SO post contains a succinct description of how to implement shared preferences. How to use SharedPreferences in Android to store, fetch and edit values

Community
  • 1
  • 1
Plastic Sturgeon
  • 12,527
  • 4
  • 33
  • 47
0

You have to create some sort of "data" or "data file". This data will have the current level. You can save it internally (onto user's phone's memory) or externally on some server. Here is what you'll need:

http://developer.android.com/guide/topics/data/data-storage.html

You want a way for your game to look to see if there is a game data already stored. Make sure you do this check before game starts (or when load game is selected). The data file could be a very simple file with just time last played and current level.

//pseudo code
//public onStart()
    //look for saved file
    //if saved file does not exist or is null
        //new game
    //else
        //parse saved file
        //load up proper level/stats/etc.

This is also useful for saving other data:

http://developer.android.com/training/basics/data-storage/index.html