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/b3914417-fcc4-4e3d-99d2-e7f92a939c40%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.