Android: 이전 액티비티로 돌아가기
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
Android: Go back to previous activity
Android: 이전 액티비티로 돌아가기
문제 내용
I want to do something simple on android app. How is it possible to go back to a previous activity.
안드로이드 앱에서 간단한 작업을 하려고 합니다. 이전 액티비티로 돌아가는 방법이 어떻게 가능한가요?
What code do I need to go back to previous activity
이전 활동으로 돌아가기 위해 필요한 코드는 무엇인가요?
높은 점수를 받은 Solution
Android activities are stored in the activity stack. Going back to a previous activity could mean two things.
안드로이드 액티비티는 액티비티 스택에 저장됩니다. 이전 액티비티로 돌아가는 것은 두 가지 의미를 가질 수 있습니다.
- You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.
- Keep track of the activity stack. Whenever you start a new activity with an intent you can specify an intent flag like
FLAG_ACTIVITY_REORDER_TO_FRONT
orFLAG_ACTIVITY_PREVIOUS_IS_TOP
. You can use this to shuffle between the activities in your application. Haven't used them much though. Have a look at the flags here: http://developer.android.com/reference/android/content/Intent.html
1. startActivityForResult로 다른 활동에서 새 활동을 열었을 경우 코드에서 finishActivity() 함수를 호출하면 이전 활동으로 돌아갈 수 있습니다.활동 스택을 추적하세요. 새 인텐트로 새 활동을 시작할 때 2. FLAG_ACTIVITY_REORDER_TO_FRONT 또는 FLAG_ACTIVITY_PREVIOUS_IS_TOP 같은 인텐트 플래그를 지정할 수 있습니다. 이를 사용하여 응용 프로그램 내의 활동 사이를 이동할 수 있습니다. 그러나 이들을 많이 사용하지는 않았습니다. 여기에서 플래그를 확인하세요: http://developer.android.com/reference/android/content/Intent.html
As mentioned in the comments, if the activity is opened with startActivity()
then one can close it with finish()
. If you wish to use the Up button you can catch that in onOptionsSelected(MenuItem item)
method with checking the item ID against android.R.id.home
unlike R.id.home
as mentioned in the comments.
댓글에서 언급했듯이 startActivity()로 활동을 열었을 경우 finish()를 사용하여 닫을 수 있습니다. Up 버튼을 사용하려면 onOptionsSelected(MenuItem item) 메소드에서 항목 ID를 android.R.id.home 대신 R.id.home에 대한 댓글에서 언급한 것과 같이 확인할 수 있습니다.
가장 최근 달린 Solution
There are few cases to go back to your previous activity:
이전 액티비티로 돌아가는 몇 가지 경우가 있습니다:
Case 1: if you want take result back to your previous activity then ActivityA.java
사례 1: 결과를 이전 활동으로 가져가려면 ActivityA.java를 사용하세요.
Intent intent = new Intent(ActivityA.this, FBHelperActivity.class);
startActivityForResult(intent,2);
FBHelperActivity.java
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
Case 2: ActivityA --> FBHelperActivity---->ActivityA
사례 2: ActivityA --> FBHelperActivity---->ActivityA
ActivityA.java
Intent intent = new Intent(ActivityA.this, FBHelperActivity.class);
startActivity(intent);
FBHelperActivity.java
after getting of result call finish();
By this way your second activity will finish and because
you did not call finish() in your first activity then
automatic first activity is in back ground, will visible.
출처 : https://stackoverflow.com/questions/4038479/android-go-back-to-previous-activity