2012-02-29 3 views
0

빠른 질문 : activitygroup이 있습니다. 해당 활동 그룹 내에서 활동이 있습니다. 이 활동 안에있는 동안 뒤로 누르면. 액티비티의 onBackPressed 메서드가 호출됩니다. - OnBackPressed의 액티비티 그룹이 아닙니다. - 왜 그런가요?ActivityGroups에서 onBackPressed() 비헤이비

편집 : 내 대답은 있지만 문제는 남아 있습니다. 다음은 코드 및 내 원래 문제에 대한 설명입니다.

TabHost 내에서 ActivityGroups를 사용하고 있으므로 onBackPressed를 재정의하도록 "강제 적용"되었습니다. 내 전화를 다시 누르고 내 탭 호스트의 탭을 눌러 문제없이 애플리케이션을 탐색 할 수 있습니다. 하지만 Back을 누르면 인터페이스와 상호 작용할 수 없습니다. 탭 호스트의 탭 중 하나를 다시 누르면 일반과 같은 모든 기능을 사용할 수 있습니다. 왜 이런 일이 일어나는 걸까요? onResume을 재정의해야합니까?

관련 코드

SettingsActivityGroup :

public class SettingsActivityGroup extends ActivityGroup 
{ 
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view 
public static SettingsActivityGroup group; 

// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory. 
private ArrayList<View> history; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    // Allocate history 
    this.history = new ArrayList<View>(); 

    // Set group 
    group = this;    

    // Start root (first) activity 
    Intent myIntent = new Intent(this, SettingsActivity.class); // Change to the first activity of your ActivityGroup 
    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    ReplaceView("SettingsActivity", myIntent); 
} 

/* 
* Replace the activity with a new activity and add previous one to history 
*/ 
public void ReplaceView(String pId, Intent pIntent) 
{ 
    Window window = getLocalActivityManager().startActivity(pId, pIntent); 
    View view = (window != null) ? window.getDecorView() : null; 

    // Add the old activity to the history 
    history.add(view); 

    // Set content view to new activity 
    setContentView(view); 
} 

/* 
* Go back from previous activity or close application if there is no previous activity 
*/ 
public void back() 
{ 
    if(history.size() > 1) 
    { 
     // Remove previous activity from history 
     history.remove(history.size()-1); 

     // Go to activity 
     View view = history.get(history.size() - 1); 
     Activity activity = (Activity) view.getContext(); 

     // "Hack" used to determine when going back from a previous activity 
     // This is not necessary, if you don't need to redraw an activity when going back 
     activity.onWindowFocusChanged(true); 
     // Set content view to new activity 
     setContentView(view); 
    } 
    else 
    { 
     // Close the application 
     finish(); 

    } 
} 
/* 
* Overwrite the back button 
*/ 
@Override 
public void onBackPressed() 
{ 
    // Go one back, if the history is not empty 
    // If history is empty, close the application 
    SettingsActivityGroup.group.back(); 

    return; 
} 
} 

임의 아이 SettingsActivityGroup의 (CallForwardActivity)

public class CallForwardActivity extends ListActivity 
{ 
.... 
    @Override 
    public void onBackPressed() 
    { 
     // Go one back, if the history is not empty 
     // If history is empty, close the application 
     SettingsActivityGroup.group.back(); 

     return; 
    } 

} 
+0

아무 것도 이상하지 않습니다. 이것을 처리하기 위해 관심을 표명했다면 활동은 그것을 처리해야합니다. else 컨트롤은 활동의 부모 인 활동 그룹을 전달할 것입니다. – jeet

답변

2

나는() 현재 선택 활동의 onBackPressed를 호출하여 원하는 동작입니다 믿기 때문에.

ActivityGroup이 더 이상 사용되지 않음에도 유의해야하지만, 사용자가 < 3.0을 코딩한다고 가정하고 지원 라이브러리 작업에 만족하지 않습니다. 편집 된 질문에 대해서는

: 이 사이트에 또 다른 질문은 좋은 ActivityGroup 예로서이 문서를 인용, 나는 http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html 이 예제는 그냥 누르면 현재 활동에 대한 마무리()를 호출하고, OS를 할 수 있습니다 동의 이전 작업을 다시 시작하십시오. 이는 이전 작업보다 간단하며 잘 작동됩니다. 자식 액티비티에서 getParent()를 호출하여 해당 정적 참조를 사용하지 않아도됩니다. (그렇게 쉽게 읽을 수 있습니다!).

+0

사실입니다. 필자의 목표는 2.2이며 필자는 지원 라이브러리를 사용하기 시작했지만 ActivityGroups에서 Fragments로 변환하는 것은 너무 많은 작업이 될 것이라고 판단했습니다. – CodePrimate

+0

이제 내가 네게있다. 나는 이전 활동으로 응용 프로그램을 전환하는 내 childActivity에서 onBackPressed()를 덮어 썼습니다. 불행히도이 활동은 "비활성"상태 인 것처럼 보이기 때문에 모든 것을 볼 수 있지만 아무 것도 누를 수는 없습니다. 다른 탭으로 전환 한 다음 다시 활동을 다시 활성화합니다. 생각? – CodePrimate

+0

나 또는 다른 사람이 문제를 발견 할 수있는 코드를 사용하여 질문을 편집하십시오. – TheLastBert

관련 문제