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.
Asked
Active
Viewed 279 times
0
-
1can you show your code... – Rathan Kumar Dec 22 '14 at 06:15
3 Answers
3
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);
}

Sayem
- 4,891
- 3
- 28
- 43
-
Ya. I can get the values by saving in bundle objects and retrieving them. But what I need is to stop number generation on rotation of screen. It has to be generated only once. Either for portrait or for landscape. – Surendar Dec 22 '14 at 07:29
-
This is basic programming.In that case you can set a flag whether it is already generated or not.if not then generate it otherwise use existing value. its all your programming logic. – Sayem Dec 22 '14 at 09:47
-
By setting flag just a method can be blocked from regenerating. But i need to stop the whole process or activity from regeneration. – Surendar Dec 24 '14 at 05:37
-
If you give me any visualization, then it will be helpful for me to understand what you really want to do. – Sayem Dec 24 '14 at 05:39
-
In my main class I have 2 random number generation, which are set to 2 text views and the sum of that 2 numbers is entered by user in edit text field. For this activity, I have made one separate portrait layout design and landscape layout design with same layout names placed in different layout folder names(design having text watch alerts and other tools). This is my activity. – Surendar Dec 24 '14 at 11:22
-
Made an update for your scenario. I think this will help you what you want. – Sayem Dec 24 '14 at 12:46
-
Thank you for spending your time. In this case, all layouts are perfectly placed and saved values are again set to text views. But the activity is recreated. I don't want it to be recreated. I want to completely stop the recreation, instead of saving and retrieving values as you did in your above code. And also I just gave you a approximate explanation about my project and the real project has so many text views with their random numbers and many alert dialogues through focus change, text watcher listeners, etc., – Surendar Dec 24 '14 at 13:17
-
When you orient device activity always recreated. you can not do anything about this & that's why android has this kind of overridden method. See when you go to landscape it loaded landscape layout. That's why it need to recreated because it is fully another view container. If you do not want to destroy your these kind of feature then you can use fragment. Try to learn it. It will help. – Sayem Dec 24 '14 at 14:32
1
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");
}
}

mbmc
- 5,024
- 5
- 25
- 53
-
I already tried this. This is just for saving and retrieving some values. But i need the whole activity unchanged on orientation change. – Surendar Dec 24 '14 at 05:38
0
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");
}

Rathan Kumar
- 2,567
- 2
- 17
- 24