Ramona
April 4, 2018, 12:29pm
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
.
newIntent(packageContext, CheatActivity.class);
->
new Intent(packageContext, CheatActivity.class);
Ramona
April 4, 2018, 4:44pm
3
Great 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
The code you listed should execute in the following order.
As part of the onCreate
method in QuizActivity
, an anonymous OnClickListener
is attached to mCheatButton
.
The user clicks the cheat button, which calls the onClick
method of the anonymous OnClickListener
.
The onClick
method calls the newIntent
static method to get an Intent
suitable for starting CheatActivity
.
The onClick
method calls startActivity
, which tells the Android system to start an instance of the activity pointed to by the Intent
.
As part of the process of creating a CheatActivity
instance, that activity’s onCreate
method is called.
Ramona
April 4, 2018, 6:14pm
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…
Look at eachBetween activity and from when the operating system level, in the end what happened. First of all, when you click on the desktop GeoQuiz application launcher, the operating system does not start the application, but only a start activity applications. Rather, it starts the application launcher activity. In GeoQuiz applications, QuizActivity is its launcher activity.