PROGRAMMING

기기 회전 시 데이터 저장 본문

Android

기기 회전 시 데이터 저장

Raccoon2125 2020. 11. 5. 11:51
package com.example.a2011005_3;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    public static final String PLAYER_LEVEL = "playerLevel";
    public static final String PLAYER_SCORE = "playerScore";
    TextView textLevel, textScore;
    Button btnLevelUp, btnScoreUp;
    int mLevel = 0, mScore= 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textLevel = findViewById(R.id.textLevel);
        textScore = findViewById(R.id.textScore);
        btnLevelUp = findViewById(R.id.button);
        btnScoreUp = findViewById(R.id.button2);

        btnLevelUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLevel++;
                textLevel.setText("레벨: "+mLevel);
            }
        });

        btnScoreUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mScore++;
                textScore.setText("점수: "+mScore);
            }
        });

        if(savedInstanceState == null) {
        } else {
            mLevel = savedInstanceState.getInt(PLAYER_LEVEL);
            mScore = savedInstanceState.getInt(PLAYER_SCORE);
            textLevel.setText("레벨: "+mLevel);
            textScore.setText("점수: "+mScore);
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt(PLAYER_LEVEL, mLevel);
        outState.putInt(PLAYER_SCORE, mScore);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textLevel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="레벨: 0" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="레벨 증가" />

    <TextView
        android:id="@+id/textScore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="점수: 0" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="점수 증가" />
</LinearLayout>

'Android' 카테고리의 다른 글

안드로이드 참고-1  (0) 2020.11.15
텍스트 저장, 삭제, 색변경  (0) 2020.11.05
데이터 저장(이메일) & onDestroy()  (0) 2020.11.05
CustomListView  (0) 2020.11.04
ListViewEdit  (0) 2020.11.04
Comments