일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 운영체제 멀티 태스킹
- IT 포지션 변경
- 운영체제 공룡책
- spring exceptionHandler response cause
- 운영체제 멀티 프로그래밍
- 백엔드 포지션 변경
- 개발 포지션 변경
- 운영체제 자원관리
- 운영체제 다중모드
- spring 동적 쿼리 주의사항
- spring responseEntity response stackTrace
- 운영체제 개념
- 개발 직무 변경
- ec2 scp 파일 전송
- spring sql injection 방지
- spring exceptionHandler reposnse stackTrace
- spring exception stackTrace remove
- IT 직무 변경
- OS 자원관리
- spring exception cause remove
- 운영체제 작동방식
- spring sql injection
- 백엔드 직무 변경
- android 타이머
- aws ec2 scp 파일 전송
- Android Timer
- spring responseEntity response cause
- spring paging sort sql injection
- 운영체제 커널모드
- spring dynamic query sql injection
- Today
- Total
오늘도 삽질중
Android Fragment Save State 본문
Android OS 단에서 메모리가 부족하든, 기타 사유로 인해 프래그먼트가 종료되는 경우
프래그먼트의 상태를 저장하는 블로그중에 괜찮은 관련 자료 링크를 모아봤다.
https://riptutorial.com/android/example/16231/saving-and-restoring-fragment-state
Android Tutorial => Saving and Restoring Fragment State
Learn Android - Saving and Restoring Fragment State
riptutorial.com
https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en
The Real Best Practices to Save/Restore Activity's and Fragment's state. (StatedFragment is now deprecated)
Months ago I published an article related to Fragment State saving & restoring, Probably be the best way (?) to save/restore Android Fragment’s state so far. A lot of valuable feed
inthecheesefactory.com
첫번째 블로그의 경우 supportFragmentManager 를 사용할 경우 좋은 방법으로 보여진다.
하지만 ViewPager 에 FragmentList를 담아서 처리하는 경우도 있을것이다.
아래는 직접 사용해본 코드이다.
참고로 테스트는 개발자옵션 -> 활동유지 안함을 체크 후 시도해봤다.
(더 좋은 방법이나 의견 또는 피드백 환영합니다. 해당 문제를 해결하기 위해 여러방법을 써봤는데 참 어렵네요...)
MainActivity.kt
private var fragments: MutableList<Fragment> = mutableListOf()
private var suggestionMainFragment: SuggestionMainFragment? = null
private var exploreMainFragment: ExploreMainFragment? = null
private var settingMainFragment: SettingMainFragment? = null
private var mainViewPagerAdapter: MainViewPagerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
if (savedInstanceState != null){
suggestionMainFragment = supportFragmentManager.getFragment(savedInstanceState , SUGGESTION_TAG) as? SuggestionMainFragment
exploreMainFragment = supportFragmentManager.getFragment(savedInstanceState , EXPLORE_TAG) as? ExploreMainFragment
settingMainFragment = supportFragmentManager.getFragment(savedInstanceState , SETTING_TAG) as? SettingMainFragment
if (suggestionMainFragment == null) suggestionMainFragment = SuggestionMainFragment.newInstance()
if (exploreMainFragment == null) exploreMainFragment = ExploreMainFragment.newInstance()
if (settingMainFragment == null) settingMainFragment = SettingMainFragment.newInstance()
initMainViewPager()
} else {
suggestionMainFragment = SuggestionMainFragment.newInstance()
exploreMainFragment = ExploreMainFragment.newInstance()
settingMainFragment = SettingMainFragment.newInstance()
initMainViewPager()
}
}
private fun initMainViewPager(){
fragments.add(suggestionMainFragment!!)
fragments.add(exploreMainFragment!!)
fragments.add(settingMainFragment!!)
mainViewPagerAdapter = MainViewPagerAdapter(this , fragments)
mainViewPagerAdapter?.also { binding.mainViewPager.adapter = it }
binding.mainViewPager.isUserInputEnabled = false
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
if (suggestionMainFragment!!.isAdded){
supportFragmentManager.putFragment(outState , SUGGESTION_TAG , suggestionMainFragment!!)
}
if (exploreMainFragment!!.isAdded){
supportFragmentManager.putFragment(outState , EXPLORE_TAG , exploreMainFragment!!)
}
if (settingMainFragment!!.isAdded){
supportFragmentManager.putFragment(outState , SETTING_TAG , settingMainFragment!!)
}
}
companion object {
const val SUGGESTION_TAG = "tag_suggestion"
const val EXPLORE_TAG = "tag_explore"
const val SETTING_TAG = "tag_setting"
}
'안드로이드' 카테고리의 다른 글
Android Gradient rounded corner programmatically (0) | 2021.12.22 |
---|---|
Android View 의 절대좌표가 맞지 않을경우 (0) | 2021.12.21 |
Android PopupMenu 백그라운드+텍스트 컬러 변경 (0) | 2021.12.18 |
Android Rxjava interval 로 타이머 만들어보기 (0) | 2021.10.14 |
Android CircleTextView 커스텀으로 구현하기 (0) | 2021.09.30 |