티스토리 뷰

반응형

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

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

 

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

How to close activity and go back to previous activity in android

안드로이드에서 이전 화면으로 돌아가고 액티비티를 종료하는 방법

 문제 내용 

I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:

제가 메인 액티비티에서 버튼을 클릭하면 새로운 액티비티가 시작되도록 하였고, 다음과 같은 코드를 사용하였습니다:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);

 

The above code was run from the main activity.

위 코드는 메인 액티비티에서 실행되었습니다.

 

Now in my new activity which is called by the main activity, I have a back button. When I click on this back button I want my new activity to close and it must go back to the original main activity.

그리고 이제 메인 액티비티에서 호출된 새로운 액티비티에는 뒤로 가기 버튼이 있습니다. 이 버튼을 클릭하면 새로운 액티비티가 닫히고 원래의 메인 액티비티로 돌아가야 합니다.

 

I have tried calling super.finish() and just finish() (from the new activity) but this then closes my entire application (including my main activity).

새로운 액티비티에서 super.finish()나 단순히 finish()를 호출해 보았지만, 이렇게 하면 내 전체 애플리케이션(메인 액티비티 포함)이 닫힙니다.

 

How can I just close the activity that is currently in focus, and then return to the main activity?

현재 포커스된 액티비티만 종료하고 메인 액티비티로 돌아가려면 어떻게 해야 할까요?

 

 

 

 높은 점수를 받은 Solution 

I think you are calling finish() method in MainActivity before starting SettingsActivity.

SettingsActivity를 시작하기 전에 MainActivity의 finish() 메서드를 호출하는 것 같습니다.

 

The scenario which you have described will occur in following two ways:

설명한 시나리오는 다음과 같은 두 가지 방법으로 수행됩니다:

 

EITHER

어느 하나

 

You have set android:noHistory = "true" for MainActivity inside AndroidManifest.xml which causes MainActivity to finish automatically on pressing the back key.

AndroidManifest.xml 내부의 MainActivity에 대해 Android:noHistory = "true"를 설정했습니다. 이로 인해 MainActivity는 뒤로 키를 누르면 자동으로 종료됩니다.

 

OR

OR

 

Before switching to your 'SettingsActivity', you have called finish() in your MainActivity, which kills it. When you press back button,since no other activity is preset in stack to pop, it goes back to main screen.

'설정 액티비티'로 전환하기 전에 메인 액티비티에서 finish()를 호출하여 작업을 중지했습니다. 뒤로 버튼을 누르면 다른 액티비티가 팝업을 위해 스택에 미리 설정되어 있지 않기 때문에 메인 화면으로 돌아갑니다.

 

 

 

 가장 최근 달린 Solution 

I believe your second activity is probably not linked to your main activity as a child activity. Check your AndroidManifest.xml file and see if the <activity> entry for your child activity includes a android:parentActivityName attribute. It should look something like this:

저는 여러분의 두 번째 액티비티가 아마도 자식 액티비티로서의 메인 액티비티와 관련이 없다고 생각합니다. AndroidManifest.xml 파일을 확인하고 자식 액티비티에 대한 항목에 Android:parentActivityName 속성이 포함되어 있는지 확인하세요.
다음과 같이 보여야 합니다:

 

<?xml ...?>
...
<activity
    android:name=".MainActivity"
    ...>
</activity>
<activity
    android:name=".ChildActivity"
    android:parentActivityName=".MainActivity"
    ...>
</activity>
...

 

 

출처 : https://stackoverflow.com/questions/15393899/how-to-close-activity-and-go-back-to-previous-activity-in-android

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