ide/androidstudio

초기화 값 저장/호출 SharedPreferences

C/H 2017. 1. 23. 08:30

public class MainActivity extends AppCompatActivity {
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        init();
    }

    @Override
    protected void onResume() {
        SharedPreferences pref = getSharedPreferences("gostop",Activity.MODE_PRIVATE);
        keep = pref.getInt("keep",0);

        super.onResume();
    }

    @Override
    protected void onPause() {

        SharedPreferences pref = getSharedPreferences("lottoSetting", MainActivity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("keep",keep);
        editor.commit();

        super.onPause();
    }
    ....
}


반응형

'ide > androidstudio' 카테고리의 다른 글

Unable to start activity ComponentInfo, on a null object reference  (0) 2017.01.25
Android 광고, ANDROID_ID  (0) 2017.01.24
네트워크 상태로 자동 실행  (0) 2017.01.20
Android Swipe  (0) 2017.01.19
Android Drawable Shape Syntax  (0) 2017.01.18