티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Android: Clear the back stack

Android: 백스택 지우기

 문제 내용 

In Android I have some activities, let's say A, B, C.

저는 안드로이드에서 A, B, C와 같은 몇 가지 액티비티가 있습니다.

 

In A, I use this code to open B:

A에서, 저는 B를 열기 위해 이 코드를 사용합니다:
Intent intent = new Intent(this, B.class);
startActivity(intent);

 

In B, I use this code to open C:

B에서, 저는 C를 열기 위해 이 코드를 사용합니다:
Intent intent = new Intent(this, C.class);
startActivity(intent);

 

When the user taps a button in C, I want to go back to A and clear the back stack (close both B and C). So when the user use the back button B and C will not show up, I've been trying the following:

사용자가 C에서 버튼을 누르면, 저는 A로 돌아가서 백스택을 지우고 싶습니다(B와 C를 모두 닫습니다). 그래서 사용자가 뒤로 버튼을 눌러도 B, C가 나타나지 않을 것 입니다. 저는 다음을 시도해 보았습니다.
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent);

 

But B and C are still showing up if I use the back button when I'm back in activity A. How can I avoid this?

하지만 제가 액티비티 A로 돌아왔을 때 뒤로 버튼을 누르면 B와 C는 여전히 나타납니다. 어떻게 하면 피할 수 있을까요?

 

 

 

 높은 점수를 받은 Solution 

Try adding FLAG_ACTIVITY_NEW_TASK as described in the docs for FLAG_ACTIVITY_CLEAR_TOP:

FLAG_ACTIVITY_CLEAR_TOP 문서에 설명된 대로 FLAG_ACTIVITY_NEW_TASK를 추가해 보세요.

 

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

이 시작 모드는 FLAG_ACTIVITY_NEW_TASK와 함께 좋은 효과를 내기 위해 사용될 수도 있습니다. 태스크의 루트 액티비티를 시작하는 데 사용되는 경우 해당 태스크의 현재 실행 중인 인스턴스를 포그라운드로 가져온 다음 루트 상태로 지웁니다. 이는 예를 들어 알림에서 액티비티를 시작할 때 특히 유용합니다.

 

So your code to launch A would be:

따라서 A를 실행하기 위한 코드는 다음과 같습니다.
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(intent);
CurrentActivity.this.finish(); // if the activity running has it's own context


// view.getContext().finish() for fragments etc.

 

 

 가장 최근 달린 Solution 

Advanced, Reuseable Kotlin:

재사용 가능한 고급 Kotlin:

 

You can set the flag directly using setter method. In Kotlin or is the replacement for the Java bitwise or |.

setter 메서드를 사용하여 플래그를 직접 설정할 수 있습니다. Kotlin에서 or는 Java 비트 또는 |를 대체합니다.
intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK

 

If use this more than once, create an Intent extension function

이 기능을 두 번 이상 사용할 경우 Intent 확장 함수를 만듭니다.
fun Intent.clearStack() {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}

 

You can then directly call this function before starting the intent

그런 다음 인텐트를 시작하기 전에 이 함수를 직접 호출할 수 있습니다.
intent.clearStack()

 

If you need the option to add additional flags in other situations, add an optional param to the extension function.

다른 상황에서 플래그를 추가하는 옵션이 필요한 경우 확장 함수에 옵션 매개 변수를 추가합니다.
fun Intent.clearStack(additionalFlags: Int = 0) {
    flags = additionalFlags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}

 

 

출처 : https://stackoverflow.com/questions/5794506/android-clear-the-back-stack

반응형
댓글
공지사항
최근에 올라온 글