Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getBooleanExtra() using only the default argument and not the one passed by putExtra()

Here's the code. In this part, the answerIsTrue variable should be initialized to true, which it rightly does (I debugged and checked) and is rightly also passed into putExtra() (again, I debugged and checked).

mCheatButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent i = new Intent(QuizActivity.this, CheatActivity.class);
            boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
            i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
            startActivity(i);
        }
    });

But coming to a different class, the variable mAnswerIsTrue gets assigned to false (probably due to the default argument) despite the argument being passed by putExtra() is true. Here's the code.

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

I debugged this line as well, and it does get assigned to false. What could be wrong?

Here's the complete CheatActivity class:

public class CheatActivity extends Activity {
public static final String EXTRA_ANSWER_IS_TRUE = "com.bignerdranch.android.geoquiz.answer_is_true";
private Button mShowAnswerButton;
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);
    mAnswerIsTrue = getIntent().getBooleanExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, false);
    mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
    mShowAnswerButton = (Button)findViewById(R.id.showAnswerButton);
    mShowAnswerButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            if (mAnswerIsTrue){
                mAnswerTextView.setText(R.id.true_button);
            }else{
                mAnswerTextView.setText(R.id.false_button);
            }
        }
    });
}

}

Note: I'm a complete beginner, who just learnt debugging.

like image 712
Slay Avatar asked Jun 26 '14 09:06

Slay


People also ask

What is getBooleanExtra?

When you Intent a class, you can send some parameters (name:value pair). In your source, putExtra -> getBooleanExtra is doing that. And getXXXExtra has default value if there is no name:value pair in parameters. So, mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

What is put EXTRA in Intent?

Using putExtra() and getExtras() in android Intents are asynchronous messages which allow Android components to request functionality from other components of the Android system. For example an Activity can send an Intents to the Android system which starts another Activity .

What is the Intent in java?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities.

What is Intent protocol?

An intent allows you to start an activity in another app by describing a simple action you'd like to perform (such as "view a map" or "take a picture") in an Intent object.


Video Answer


1 Answers

I cannot be sure, but my best guess that, getBooleanExtra() is not good. I suggest using simple getExtras and then getting your value.

 i.putExtra(EXTRA_ANSWER_IS_TRUE, value);

 Bundle args = MyActivity.getIntent().getExtras();
 boolean istrue= args.getBoolean(EXTRA_ANSWER_IS_TRUE, false);
like image 125
Alpha Avatar answered Oct 05 '22 08:10

Alpha