앱을 만들어도 화면을 전환하면 데이터가 날라간다.
프로젝트명 : SaveRestoreTest
MainActivity.java 소스
package com.example.saverestoretest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button1, button2;
TextView text;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
button1 = (Button) findViewById(R.id.button01);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
count++;
text.setText("현재 개수 = " + count);
}
});
button2 = (Button) findViewById(R.id.button02);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
count--;
text.setText("현재 개수 = " + count);
}
});
if (savedInstanceState != null) {
count = savedInstanceState.getInt("count");
text.setText("현재 개수 = " + count);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("count", count);
}
}
activity_main.xml 소스
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="주문" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="피자" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="스파게티" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="현재 개수 = 0" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
>
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="증가"
/>
<Button
android:id="@+id/button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="감소"
/>
</LinearLayout>
</LinearLayout>