Surendar
Surendar

Reputation: 79

How to avoid random number generation on orientation change in android

I am new to android and am developing a sample application using random numbers.I am having random number generation in my activity which is applied to some of the text views in my corresponding layout. Whenever i change the orientation from portrait to landscape or landscape to portrait, the random number values are generating each time when I rotate the screen for the same activity. So, the previous values get lost and new numbers are generated. I tried many ways like android:configChanges="keyboardHidden|orientation" and, saving and restoring some of my values, but that are all not giving me a proper solution. Is there a suitable solution for this.? Thanks in advance.

Upvotes: 0

Views: 308

Answers (3)

Sayem
Sayem

Reputation: 5011

When you change the orientation Your Activity it is fully destroyed & recreated. So every time it creates new random number after orientation. There is an overridden method protected void onSaveInstanceState (Bundle outState) where you can store your values in bundle object & retrieve it oncreate.

Like :

@Override
public void onSaveInstanceState(Bundle outState) {
    int value = 0; // test value
    outState.putInt("key",value);
    super.onSaveInstanceState(outState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState != null) {
        int randomInt = savedInstanceState.getInt("key");
    }
    // generate random number
}

UPDATE:

private TextView first;
private TextView second;
private EditText editText;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initializeViews();
    if (savedInstanceState == null) {
        first.setText(getRandom());
        second.setText(getRandom());
    }
    else {
        first.setText(savedInstanceState.getString("first"));
        second.setText(savedInstanceState.getString("second"));
    }
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int firstValue = Integer.parseInt(first.getText().toString());
            int secondValue = Integer.parseInt(second.getText().toString());
                int result = Integer.parseInt(editText.getText().toString());
                if( firstValue + secondValue == result )
                {
                    Toast.makeText(MainActivity.this, "Matched", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Wrong, try again", Toast.LENGTH_SHORT).show();
                }
            }
    });
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("first",first.getText().toString());
    outState.putString("second",second.getText().toString());
    super.onSaveInstanceState(outState);
}

private String getRandom() {
    return  Integer.toString((int)(Math.random()*10));
}

private void initializeViews() {
    first = (TextView)findViewById(R.id.first);
    second = (TextView)findViewById(R.id.second);
    editText = (EditText)findViewById(R.id.editText);
    button = (Button)findViewById(R.id.button);
}

Upvotes: 3

Rathan Kumar
Rathan Kumar

Reputation: 2577

try like this:

int number;
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("key", number);
        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        number=savedInstanceState.getInt("key");
    }

Upvotes: 0

mbmc
mbmc

Reputation: 5105

protected void onSaveInstanceState (Bundle outState) {
    outState.putInt("random", randomInt);
    super.onSaveInstanceState(outState)
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceSate != null) {
        randomInt = savedInstanceState.getInt("random");
    }
 }

Upvotes: 1

Related Questions