0

I'm trying to make a simple sudoku but I have a problem with the Continue button. When you first start the app, the button is disabled with:

continueButton.setEnabled (false);

Thus leading the user to have to use NewGame button. That button automatically enabled the Continue button with:

continueButton.setEnabled (true);

The problem is that, closing the app, using the exit button or the back button of the phone, I lose the status of the Continue button, that when restart the app, is again disabled.

This is my code:

public class Sudoku extends Activity implements OnClickListener {
private static final String TAG = "Sudoku";
private static final String PROFILE = "stato";
boolean t = false;
SharedPreferences preferences;
int  a , b = 0, c = 0;
View continueButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Set up click listeners for all the buttons
continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);

t = isFirstRun();
if (t == false){
continueButton.setEnabled(false);
}else{
continueButton.setEnabled(true);
}
}


@Override
protected void onResume() {
super.onResume();
Music.play(this, R.raw.main);
}

@Override
protected void onPause() {
super.onPause();
Music.stop(this);

}

public void savePreferences(){
SharedPreferences.Editor ed = preferences.edit();
ed.putBoolean("button", true);
ed.commit();
}

public boolean isFirstRun(){
preferences = PreferenceManager.getDefaultSharedPreferences(this);   
return preferences.getBoolean("button", t);

}


//onClick method

public void onClick(View v) {
switch (v.getId()) {
case R.id.continue_button:
a = 1;
startGame(Game.DIFFICULTY_CONTINUE);
break;
// ...
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
// More buttons go here (if any) ...
case R.id.new_button://button NewGame when pressed enable continueButton
continueButton.setEnabled(true);
a = 0;

openNewGameDialog();
break;
case R.id.exit_button: //exit button


finish();


break;
}
}



@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{       
finish();

}

return super.onKeyDown(keyCode, event);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
// More items go here (if any) ...
}
return false;
}

/** Ask the user what difficulty level they want */
private void openNewGameDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.new_game_title)
.setItems(R.array.difficulty,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,
int i) {
startGame(i);
}
})
.show();
}

/** Start a new game with the given difficulty level */
@SuppressLint("ParserError")
private void startGame(int i) {

Log.d(TAG, "clicked on " + i);
Intent intent = new Intent(this, Game.class);
intent.putExtra("blocca", a);
intent.putExtra(Game.KEY_DIFFICULTY, i);
startActivity(intent);
}
}
whiteTIGER
  • 391
  • 1
  • 6
  • 19
  • 1
    checkout the storage options listed in http://developer.android.com/guide/topics/data/data-storage.html You could start by using SharedPreferences in your app. You can also checkout this thread: http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state – hovanessyan Jul 10 '12 at 15:30

2 Answers2

1

Take a look at android's Data Storage options. I would recommend simply saving a boolean with SharedPreferences and then check its value every time your main activity is created.

public class MainActivity extends Activity{

    //Class used to retain data
    private SharedPreferences preferences;
    private boolean isFirstRun;

    //initialize the preferences in onCreate()
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        isFirstRun = false;
    }

    //When your application gains the foreground, check to see if it is first time
    @Override
    public void onResume(){
        //if nothing has been stored, returns true since it is the first run.
        isFirstRun = preferences.getBoolean("first",true);
        if(isFirstRun)
            //disable continue button
        else
            //enable continue button
    }

    //Save preferences anytime your application loses the foreground
    @Override
    public void onPause(){
        SharedPreferences.Editor ed = preferences.edit();
            ed.putBoolean("first",isFirstRun);
            ed.commit();
    }
}
Joel
  • 4,732
  • 9
  • 39
  • 54
  • I can not understand how to use your advice could be more clear? I added these lines of code in my app but nothing happens – whiteTIGER Jul 10 '12 at 16:00
  • see edit, added comments and a class. Do you understand what we are doing here? This is how it should be done. – Joel Jul 10 '12 at 16:13
  • no need for separate methods, just put the code in the activity methods like I did ^ – Joel Jul 10 '12 at 16:24
  • works well, but now if I open the app and close it immediately without pressing any other buttons, at the return in the app I find the continue button enabled without ever having started a game at sudoku. – whiteTIGER Jul 10 '12 at 16:34
  • my bad. In the onPause() method, simply change the middle line to... ed.putBoolean("first",isFirstRun); That should work. – Joel Jul 10 '12 at 16:40
  • Now it is always disabled :-( – whiteTIGER Jul 10 '12 at 16:45
  • Well, what you need to do, is if the user starts a new game, you need to set isFirstRun to false. Its now your job to maintain that variable and change it as necessary. – Joel Jul 10 '12 at 16:47
  • Thank you very much , you have been of great help :-) – whiteTIGER Jul 10 '12 at 16:56
  • Anytime! Best of luck to ya! :) – Joel Jul 10 '12 at 16:57
0

You can use the Shared Preference and check the status of your variables in onResume or OnPause methods

These variables are kept alive while not uninstall or delete the application data

To create a variable with SharedPreferences

SharedPreferences getSharedPreferences prefs = (PROFILE, Context.MODE_PRIVATE);

SharedPreferences.Editor prefs.edit editor = ();
editor.putBoolean ("ContinueButtonValue", false);
editor.commit ();

and to recover its value after a pause of the your application:

SharedPreferences prefs = getSharedPreferences (Settigns, Context.MODE_PRIVATE);          
prefs.getBoolean sConfiguration = prefs.getBoolean ("ContinueButtonValue", false);
Darry Morales
  • 171
  • 1
  • 8