0

I'm a beginner when it comes to Android Studio/Java, so please forgive me if the question is kind of silly.

I have almost finished my first app, which is a simple riddle game; a question is being asked, and with the right answer you get to the next activity - and so on. In the main menu, there are two options as usual: New Game and Continue. The Continue button works like a charm using shared preferences - when pressed, it takes you to the last question you didn't answer.

My problem is: I cannot make the Continue button disabled by default, and then enabled when the first question is answered. Well, the truth is I can disable it by using

    @Override
    public void onResume() {
    super.onResume();
    setContentView(R.layout.activity_main_menu_with_logo);

    mContinueButton = (Button)findViewById(R.id.ContinueButton);
    mContinueButton.setEnabled(false);
    ...
    }

but I haven't found a way to make it enabled later. I thought that by writing

if (!(question00Answered)) {

                mContinueButton.setEnabled(true);

                Intent intent = new Intent(MainMenuWithLogo.this, Question00.class);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();

which is under "public void onClick" would make it work, but no, it doesn't. I still cannot press it even though the question was answered. So, any tips on what should I fix? Any kind of help would be greatly appreciated.

Here is the full script:

MainMenuWithLogo.Java

public class MainMenuWithLogo extends AppCompatActivity {

private Button mStartInterrogationButton;
private VideoView mLogoprwto;
private Button mContinueButton;
MediaPlayer song;


@Override
protected void onPause() {
    super.onPause();
    song.release();
}

@Override
public void onResume() {
    super.onResume();
    setContentView(R.layout.activity_main_menu_with_logo);
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    song = MediaPlayer.create(this, R.raw.chopin);
    song.start();
    song.setLooping(true);




    mLogoprwto = (VideoView) findViewById(R.id.logoprwto);
    mLogoprwto.setVideoPath("android.resource://its_destination/"+R.raw.teloslogo);
    mLogoprwto.start();

    mLogoprwto.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {

            mLogoprwto.start();
        }
    });

    mStartInterrogationButton = (Button)findViewById(R.id.StartInterrogationButton);
    mStartInterrogationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            startGame();

        }
    });

    mContinueButton = (Button)findViewById(R.id.ContinueButton);
    mContinueButton.setEnabled(false);

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

            SharedPreferences prefs = getSharedPreferences("Stage", MODE_PRIVATE);
            boolean question00Answered = prefs.getBoolean("QuestionZero", false);
            boolean question01Answered = prefs.getBoolean("QuestionOne", false);
            boolean question02Answered = prefs.getBoolean("QuestionTwo", false);




            if (!(question00Answered)) {

                mContinueButton.setEnabled(true);

                Intent intent = new Intent(MainMenuWithLogo.this, QuestionZero.class);

                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            } else if (!(question01Answered)) {

                mContinueButton.setEnabled(true);


                Intent intent = new Intent(MainMenuWithLogo.this, QuestionOne.class);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            } else if (!(question02Answered)) {

                mContinueButton.setEnabled(true);

                Intent intent = new Intent(MainMenuWithLogo.this, QuestionTwo.class);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            }else {

                mContinueButton.setEnabled(true);

                Intent intent = new Intent(MainMenuWithLogo.this, End.class);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            }

        }
    });

}

private void startGame () {
Intent intent = new Intent(this, Intro.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
    @Override
    public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
}
Animated24
  • 31
  • 4

1 Answers1

0

You are enabling the button on the wrong activity. Here is what your code is doing, in outline form:

  1. onClick() runs for Activity #1's mContinueButton
  2. Activity #1's mContinueButton is enabled
  3. Activity #2 is started
  4. Activity #1 (along with it's mContinueButton, which is the only enabled one at this point) is destroyed before activity #2 even opens up, b/c of the finish() call.

To fix, make your decision to call setEnabled(true) in onResume(). Remove the setEnabled(true) calls from onClick(). For example:

  1. Disable the button.
  2. Examine your preference Booleans. If any of them are false, call setEnabled(true).
  3. Set the button's onClick() handler. No calls to setEnabled() should be needed here (b/c that decision has already been made), but keep the if/then statements, and all the other code there, because you still need to choose which Activity to open.

Note that you've declared the onClick() handler as a closure. The code in that function does not run when onResume() executes; it runs when a user clicks an enabled view.

Your yellow box will not cause any problems at the moment; refer to this question for a solution.

Community
  • 1
  • 1
greeble31
  • 4,894
  • 2
  • 16
  • 30
  • I start understanding the logic behind this. So, as far as the code above is concerned, I have to write it down like this: "mContinueButton = (Button)findViewById(R.id.ContinueButton); mContinueButton.setEnabled(false); mContinueButton.setEnabled(true);" under onResume(), and remove the "mContinueButton.setEnabled(true);" lines from the onClick()? Or do I have to make changes to other activities too? Because I did what you said, but now the button is always enabled. Also, mContinueButton.setEnabled(false); appears in a yellow box: http://i.imgur.com/wfbjWPV.jpg – Animated24 Aug 30 '16 at 14:38
  • I've added more detail to the answer. – greeble31 Aug 30 '16 at 22:27