1

I am following Big Nerd Ranch guide Android in which I am building a basic application which displays a set of questions and you say true or false. There is a button 'Cheat' where in you can cheat. If you hit the button Cheat see the answer, come back and type the correct answer then there is a toast displayed 'Cheating is wrong'. This works fine in vertical screen but lets say I rotate the screen to horizontal, I view the answer and come back to horizontal, I can cheat. I dont want that to happen.

My Effort: using onSavedInstanceState to override the existing function to share values between screens & keeping track of the value in onCreate

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    super.onSaveInstanceState(savedInstanceState);
    Log.i(TAG, "onSaveInstanceState");
    savedInstanceState.putBoolean(EXTRA_ANSWER_SHOWN, mAnswerIsTrue) ;
}

In onCreate method,

if(savedInstanceState !=null)
{
 mAnswerIsTrue = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN);
 setAnswerShownResult(mAnswerIsTrue);
}

Here is my code:

package com.android.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends Activity 
{
    //Extras
    public static final String EXTRA_ANSWER_IS_TRUE = "com.android.geoquiz.answer_is_true";
    public static final String EXTRA_ANSWER_SHOWN = "com.android.geoquiz.answer_shown";

    private static final String TAG="CheatActivity";
    private static final String KEY_INDEX = "cheat";
    private boolean mAnswerIsTrue;
    private TextView mAnswerTextView;
    private Button mShowAnswer;

    private void setAnswerShownResult(boolean isAnswerShown)
    {
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);


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

        mAnswerTextView = (TextView)findViewById(R.id.showAnswerButton);
        setAnswerShownResult(false);
        mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
        mShowAnswer.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);
            }
        });



        if(savedInstanceState !=null)
        {

            mAnswerIsTrue = savedInstanceState.getBoolean(KEY_INDEX, false);

        }

    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState)
    {
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG, "onSaveInstanceState");
        savedInstanceState.putBoolean(KEY_INDEX, mAnswerIsTrue) ;
    }


    @Override
    public void onStart()
    {
        super.onStart();
        Log.d(TAG, "onStart() called");
    }
    @Override
    public void onPause()
    {
        super.onPause();
        Log.d(TAG, "onPause() called");
    }
    @Override
    public void onResume()
    {
        super.onResume();
        Log.d(TAG, "onResume() called");
    }
    @Override
    public void onStop()
    {
        super.onStop();
        Log.d(TAG, "onStop() called");
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.d(TAG, "onDestroy() called");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
    }


}
fscore
  • 2,567
  • 7
  • 40
  • 74

2 Answers2

2

This is happening because your onCreate is being called once the user rotates the screen there is simple solution to stop onCreate from being called onOrientationChanged

<activity android:name=".MyActivity" 
          android:configChanges="orientation|keyboardHidden" />

now when you do this you have to overide a method in your activity class like you have onCreate , onPause you will need to add another method named onConfigChanged(); now whenever your screen is rotated this method is gonna be fired instead of activity being created again and again.

add this to your activity class

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
    }

Hope this helps. if you need any help feel free to ask.

for more info read these posts.

Activity restart on rotation Android

How do I disable orientation change on Android?

http://developer.android.com/guide/topics/manifest/activity-element.html#config

Community
  • 1
  • 1
Umer Kiani
  • 3,783
  • 5
  • 36
  • 63
  • Can you tell me what changes to make in my code or where am I going wrong? Your answer might help but its not detailed enough to help. I did what you said but dint work. – fscore Jul 29 '14 at 08:18
  • All you need to do is add this above code to your mainifest. what issue you are facing is because once screen orientation is changed it forces to call onCreate obviously you dont want to reset all things. so put this method in your activity @Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub super.onConfigurationChanged(newConfig); } this is gona be called on your orientation changes – Umer Kiani Jul 29 '14 at 08:21
  • Still not Clear just let me know :) – Umer Kiani Jul 29 '14 at 08:29
  • I did whAT you asked again. But it dint work. Please check my updated code – fscore Jul 29 '14 at 21:38
  • is onConfigChanged Being Called ? and you have to put that mainifest code in all your activities – Umer Kiani Jul 30 '14 at 05:02
0

In your manifest add this to your activity

android:configChanges="orientation|screenSize"

And in your Activity listen for config changes

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    System.out.println(newConfig.toString());
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
         Toast.makeTest(this,"portrait",Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeTest(this,"landscape",Toast.LENGTH_SHORT).show();
    }

}
Praveena
  • 6,340
  • 2
  • 40
  • 53