Hint:
Your Activity object is destroyed and then recreated on a rotation. This
means that all your *new* Activity's fields have been set to 0/false/null
again.
Read the documentation about the `onSaveInstanceState(...)` method and how
this stores data in the `savedInstanceState` parameter of the onCreate(...).
On Tuesday, April 12, 2016 at 2:15:46 AM UTC-4, Jesse Clark wrote:
>
> I am new to Android development. I am experimenting with making a simple
> app that displays a number that increases by one when the Up button is
> pressed and decreases by one when the Down button is pressed. It works
> correctly, except when I rotate the device from landscape to portrait or
> vice versa, the number is reset to 0. Here is the code:
>
> package com.example.jz.counter;
>
> import android.app.Activity;
> import android.content.Intent;
> import android.os.Bundle;
> import android.view.View;
> import android.widget.Button;
> import android.widget.TextView;
>
> public class MainActivity extends Activity implements View.OnClickListener
> {
> private int counter = 0;
> private TextView OutputViewObject;
>
> @Override
> protected void onCreate(Bundle savedInstanceState)
> {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_main);
> OutputViewObject = (TextView)findViewById(R.id.outputView);
> OutputViewObject.setText("" + counter);
> final Button buttonUp = (Button)findViewById(R.id.up_button);
> final Button buttonDown = (Button)findViewById(R.id.down_button);
> buttonUp.setOnClickListener(this);
> buttonDown.setOnClickListener(this);
> }
>
> @Override
> public void onClick(View view)
> {
> switch (view.getId())
> {
> case R.id.up_button:
> counter++;
> OutputViewObject.setText("" + counter);
> break;
> case R.id.down_button:
> counter--;
> OutputViewObject.setText("" + counter);
> break;
> }
> }
> }
>
>
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/82e379ef-d6d5-45b3-8b0d-880f25ec2db0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.