This is the solution to my first problem. I think my logic is all right, but it doesn't work.
package com.example.geoquize;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CheatActivity extends AppCompatActivity {
private static final String EXTRA_ANSWER_IS_TRUE= “com.example.geoquize.answer_is_true”;
private static final String EXTRA_ANSWER_SHOWN=“com.example.geoquize.answer_shown”;
private static final String TAG=“CheatActivity”;
private static final String KEY_INDEX_c=“index_c”;
private boolean mAnswerIsTrue;
private boolean x=false;
private TextView mAnswerTextView;
private Button mShowAnswerButton;
public static Intent newIntent(Context packageContext, boolean answerIasTrue){
Intent intent=new Intent(packageContext,CheatActivity.class);
intent.putExtra(EXTRA_ANSWER_IS_TRUE,answerIasTrue);
return intent;
}
public static boolean wasAnswerShown(Intent result){
return result.getBooleanExtra(EXTRA_ANSWER_SHOWN,false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate(Bundle)called");
setContentView(R.layout.activity_cheat);
if(savedInstanceState !=null){
boolean y=savedInstanceState.getBoolean(KEY_INDEX_c,false);
setAnswerShownResult(y);
}
mAnswerIsTrue=getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);
mAnswerTextView=(TextView)findViewById(R.id.answer_text_view);
mShowAnswerButton=(Button)findViewById(R.id.show_answer_button);
mShowAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mAnswerIsTrue){
mAnswerTextView.setText(R.string.true_button);
}else{
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
x=true;
}
});
}
@Override
public void onStart(){
super.onStart();
Log.d(TAG,"onStart()called");
}
@Override
public void onResume(){
super.onResume();
Log.d(TAG,"onResume()called");
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG,"onPause()called");
}
@Override
public void onStop(){
super.onStop();
Log.d(TAG,"onStop()called");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG,"onDestroy()called");
}
private void setAnswerShownResult(boolean isAnswerShown){
Intent data=new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN,isAnswerShown);
setResult(RESULT_OK,data);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG,"onSaveInstanceState");
savedInstanceState.putBoolean(KEY_INDEX_c,x);
}
}