2012-02-29 7 views
0

簡単な質問:私はアクティビティグループを持っています。そのアクティビティグループ内に私はアクティビティを持っています。この活動の中で私が背中を押すと。アクティビティのonBackPressedメソッドが呼び出されます - OnBackPressedのアクティビティグループではありません - なぜですか?ActivityGroupsのonBackPressed()動作

編集:私の答えは得ましたが、問題は残ります。ここでは、私の元の問題のコードと説明を次に示します。

私は、TabHost内でActivityGroupsを使用しており、onBackPressedを無効にすることを強制されています。私の電話を押して、タブホストのタブを押すことで問題なくアプリケーションをナビゲートすることができます。しかし、Backキーを押した後はインターフェイスと対話できません。 タブホストのタブの1つをもう一度押すと、通常どおりの操作ができます。なぜこうなった? 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

何も変わっていません。これを処理するために関心を表明した場合、アクティビティはそれを処理する必要があります。そうでなければ、コントロールはアクティビティの親であるアクティビティグループに渡されます。 – jeet

答えて

2

私は()現在選択されている活動のonBackPressedを呼び出す必要な動作であると考えているため。

アクティビティグループは非推奨ですが、< 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

関連する問題