Error in newIntent (Context packageContext, boolean answerIsTrue)


#1

Hello,
here I get error

public static Intent newIntent (Context packageContext, boolean answerIsTrue){
    Intent intent = newIntent(packageContext, CheatActivity.class);
    intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    return intent;
}

Wrong 2nd argument type. Found: ‘java.lang.Class<make.geoquiz.CheatActivity>’, required: 'boolean

But this code is taken from book and there is CheatActivity.class.


#2
newIntent(packageContext, CheatActivity.class);

->

new Intent(packageContext, CheatActivity.class);

#3

Great :slight_smile: such a silly mistake I made. Thank you.
Could you also tell me what comes after what in this code?

   public class CheatActivity extends AppCompatActivity {

private static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_true";
private boolean mAnswerIsTrue;


public static Intent newIntent (Context packageContext, boolean answerIsTrue){
    Intent intent = new Intent(packageContext, CheatActivity.class);
    intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    return intent;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat_acitvity);

    mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
}

}

    public class QuizActivity extends AppCompatActivity {

mCheatButton = findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

           boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
           Intent intent = CheatActivity.newIntent(QuizActivity.this,answerIsTrue);
            startActivity(intent);
        }
    });

Im just totally lost and don’t know what executes first, what later and so on.

Could you please on this one also?
Many thanks i advance


#4

The code you listed should execute in the following order.

  1. As part of the onCreate method in QuizActivity, an anonymous OnClickListener is attached to mCheatButton.
  2. The user clicks the cheat button, which calls the onClick method of the anonymous OnClickListener.
  3. The onClick method calls the newIntent static method to get an Intent suitable for starting CheatActivity.
  4. The onClick method calls startActivity, which tells the Android system to start an instance of the activity pointed to by the Intent.
  5. As part of the process of creating a CheatActivity instance, that activity’s onCreate method is called.

#5

Great, thank you.
But if we were about to point which line starts after which this goes like this:

nr 1. boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
nr 2. ?
nr 3. ?
etc…