티스토리 뷰

반응형

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

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

 

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

How can I return to a parent activity correctly?

어떻게 부모 액티비티로 제대로 돌아갈 수 있을까요?

 문제 내용 

I have 2 activities (A and B) in my android application and I use an intent to get from activity A to activity B. The use of parent_activity is enabled:

제 안드로이드 어플리케이션에서는 2개의 액티비티 (A와 B)를 사용하고 인텐트를 사용하여 액티비티 A에서 액티비티 B로 이동합니다. parent_activity 사용이 가능합니다:
 <activity
        android:name=".B"
        android:label="B" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.app_name.A" />
  </activity>

 

I also use a theme which provides an UP-button.

UP-버튼을 제공하는 테마도 사용합니다.

 

So after I called activity B I can use the UP-button to get back to the activity A. The problem is that the application seems to call the onCreate()-function of activity A again and this is not the behaviour I need. I need activity A to look the same way like it looked before I called activity B.

그래서 액티비티 B를 호출한 후에는 UP-버튼을 사용하여 액티비티 A로 돌아갈 수 있습니다. 문제는 응용 프로그램이 액티비티 A의 onCreate() 함수를 다시 호출하는 것처럼 보인다는 것입니다. 이것은 필요한 동작이 아닙니다. 액티비티 B를 호출하기 전과 같은 모습으로 액티비티 A를 유지하려고 합니다.

 

Is there a way to achieve this?

이것을 달성하는 방법이 있을까요?

 

EDIT

편집

 

I didn't write any code to start activity B from activity A. I think it is auto-generated by Eclipse.

액티비티 A에서 액티비티 B를 시작하는 코드를 작성하지 않았습니다. Eclipse에서 자동 생성된 것 같습니다.

 

Class B looks like:

B 클래스는 다음과 같습니다.
    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_b, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

 

 

 높은 점수를 받은 Solution 

You declared activity A with the standard launchMode in the Android manifest. According to the documentation, that means the following:

당신은 안드로이드 manifest에서 standard launchMode로 액티비티 A를 선언했습니다. 문서에 따르면, 다음과 같습니다:

 

The system always creates a new instance of the activity in the target task and routes the intent to it.

시스템은 대상 태스크에서 액티비티의 새 인스턴스를 항상 생성하고 인텐트를 전달합니다.

 

Therefore, the system is forced to recreate activity A (i.e. calling onCreate) even if the task stack is handled correctly.

따라서 시스템은 태스크 스택을 올바르게 처리하더라도 액티비티 A를 다시 생성 (즉, onCreate 호출)해야 합니다.

 

To fix this problem you need to change the manifest, adding the following attribute to the A activity declaration:

이 문제를 해결하려면 manifest에 A 액티비티 선언에 다음 속성을 추가해야 합니다:
android:launchMode="singleTop"

 

Note: calling finish() (as suggested as solution before) works only when you are completely sure that the activity B instance you are terminating lives on top of an instance of activity A. In more complex workflows (for instance, launching activity B from a notification) this might not be the case and you have to correctly launch activity A from B.

참고: finish()를 호출하는 것은 (이전에 제안된 대로) activity B 인스턴스가 activity A의 인스턴스 위에 있음이 확실한 경우에만 작동합니다. 더 복잡한 워크플로우 (예: 알림에서 액티비티 B를 시작하는 경우)의 경우, 액티비티 A를 B에서 올바르게 시작해야 합니다.

 

 

 

 가장 최근 달린 Solution 

What worked for me was adding:

제 경우에는 다음을 추가했습니다.
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

 

to TheRelevantActivity.java and now it is working as expected

TheRelevantActivity.java에서 추가하고 이제 예상대로 작동합니다.

 

and yeah don't forget to add:

getSupportActionbar.setDisplayHomeAsUpEnabled(true); in onCreate() method

그리고 onCreate() 메소드에서 getSupportActionbar.setDisplayHomeAsUpEnabled(true);를 추가하는 것을 잊지 마세요.

 

 

 

출처 : https://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly

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