티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
How to restart Activity in Android
안드로이드에서 Activity를 재시작하는 방법
문제 내용
How do I restart an Android Activity
? I tried the following, but the Activity
simply quits.
안드로이드에서 Activity를 어떻게 다시 시작할 수 있을까요? 다음과 같이 시도해봤지만 Activity가 단순히 종료됩니다.
public static void restartActivity(Activity act){
Intent intent=new Intent();
intent.setClass(act, act.getClass());
act.startActivity(intent);
act.finish();
}
높은 점수를 받은 Solution
I did my theme switcher like this:
저는 이렇게 테마 스위처를 구현했습니다:
Intent intent = getIntent();
finish();
startActivity(intent);
Basically, I'm calling finish()
first, and I'm using the exact same intent this activity was started with. That seems to do the trick?
기본적으로, 제가 먼저 finish()를 호출하고, 이 activity가 시작된 것과 정확히 같은 intent를 사용합니다. 그렇게 하면 작동하는 것 같습니다?
UPDATE: As pointed out by Ralf below, Activity.recreate()
is the way to go in API 11 and beyond. This is preferable if you're in an API11+ environment. You can still check the current version and call the code snippet above if you're in API 10 or below. (Please don't forget to upvote Ralf's answer!)
업데이트: Ralf의 답변에서 지적한 대로, API 11 이상에서는 Activity.recreate()를 사용해야 합니다. API11+ 환경에서는 이것이 선호됩니다. 여전히 현재 버전을 확인하고 API 10 이하인 경우 위의 코드 스니펫을 호출할 수 있습니다. (Ralf의 답변에 투표하는 것을 잊지 마세요!)
가장 최근 달린 Solution
If anybody is looking for Kotlin answer you just need this line.
Kotlin에서 답변을 찾는다면 다음 한 줄만 필요합니다.
Fragment
startActivity(Intent.makeRestartActivityTask(activity?.intent?.component))
Activity
startActivity(Intent.makeRestartActivityTask(this.intent?.component))
출처 : https://stackoverflow.com/questions/1397361/how-to-restart-activity-in-android
'개발 > 안드로이드' 카테고리의 다른 글
'package R does not exist' 오류 수정하기 (0) | 2022.12.21 |
---|---|
Fragment에서 onBackPressed()를 구현하기 (0) | 2022.12.21 |
View 타입 객체의 setTag()와 getTag() 메서드의 주요 목적 (0) | 2022.12.20 |
XML onClick를 사용하여 Fragment에서 버튼 클릭을 처리하는 방법 (0) | 2022.12.20 |
안드로이드에서 전체 화면 액티비티 만들기 (0) | 2022.12.20 |