오늘이라도
[Android] 11. Life Cycle : onCreate, onStart, onResume, onPause, onStop, onDestroy, 데이터 저장, 불러오기 본문
취업성공패키지 SW 개발자 교육/Android
[Android] 11. Life Cycle : onCreate, onStart, onResume, onPause, onStop, onDestroy, 데이터 저장, 불러오기
upcake_ 2020. 5. 20. 09:28반응형
https://github.com/upcake/Class_Examples
교육 중에 작성한 예제들은 깃허브에 올려두고 있습니다.
gif 파일은 클릭해서 보는 것이 정확합니다.
- Life Cycle : onCreate, onStart, onResume, onPause, onStop, onDestroy, 데이터 저장, 불러오기 -
▼activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/btnMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="164dp"
android:text="새 창 띄우기"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.466"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
▼MainActivity.java
package com.example.my12_lifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate : 호출됨");
//버튼을 누르면 서브1액티비티가 나오게끔 설정
Button btnMain = findViewById(R.id.btnMain);
btnMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Sub1Activity.class);
startActivity(intent);
}
});
}
//우클릭 - Generate... - Override Methods...
//실행 하면 onCreate - onStart - onResume
//메인 버튼 누르면 onPause - onStop
//서브 버튼 누르면 onRestart - onStart - onResume
//뒤로가기 누르면 onPuase - onStop - onDestroy
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart : 호출됨");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume : 호출됨");
loadPersonal();
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop : 호출됨");
savePersonal();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy : 호출됨");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause : 호출됨");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart : 호출됨");
}
//데이터 저장하기 : onStop
public void savePersonal() {
SharedPreferences pref = getSharedPreferences("personal", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("id", "hong_gil_dong");
editor.putInt("pw", 1234);
editor.commit();
}
//저장된 데이터 불러오기 : onResume
public void loadPersonal() {
SharedPreferences pref = getSharedPreferences("personal", Activity.MODE_PRIVATE);
if (pref != null) {
String id = pref.getString("id", "CSS");
int pw = pref.getInt("pw", 0);
Log.d(TAG, "loadPersonal: " + id + ", " + pw);
}
}
}
▼activity_sub1.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".Sub1Activity">
<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="124dp"
android:text="메인으로 돌아가기"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
▼Sub1Activity.java
package com.example.my12_lifecycle;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Sub1Activity extends AppCompatActivity {
//로그캣 설정
private static final String TAG = "Sub1Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub1);
Button btnSub = findViewById(R.id.btnSub);
//누르면 창이 닫히게끔 설정
btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
반응형
'취업성공패키지 SW 개발자 교육 > Android' 카테고리의 다른 글
[Android] 13. SMS 메시지 수신 후 내용 출력 (3) | 2020.05.22 |
---|---|
[Android] 12. 서비스 (0) | 2020.05.21 |
[Android] 10. intent로 Activity 간에 데이터 주고받기, DTO 활용 / Dialog 띄우기 (0) | 2020.05.19 |
[Android] 9. Layout Inflate (0) | 2020.05.18 |
[Android] 8. Drawable Resource XML 작성 : gradient, shape, drawable, layer-list (0) | 2020.05.15 |