1

2 つのタブを持つ TabHost アクティビティがあります。さらに、私のアプリケーションには基本的に 2 種類の通知があります。ここで、通知 1 をクリックしてタブ 1 を開き、通知 2 をクリックしてタブ 2 を開きたいと考えています。タブを変更するには、TabHost アクティビティの onResume メソッドで getIntent().getExtra(...) を使用して ID を取得し、タブを変更しようとしました。このソリューションは、ほとんどの場合に機能します。ただし、正しく動作しない場合がありますが、その理由はわかりません。なぜそれが時々うまくいくのか、時々うまくいかないのか、何か手がかりはありますか? または、この問題を解決するためのより良い解決策がありますか?

4

1 に答える 1

0

これを行うには、ActivityGroups を使用する必要があります。

http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html

http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity

ただし、ActivityGroups は ICS では非推奨になっていることに注意してください。

これはActivityGroupの私の実装です:

タブ内のアクティビティ:

Intent i = new Intent(v.getContext(), SearchList.class);
i.putExtra("search", search);

View view = SearchActivityGroup.group.getLocalActivityManager()  
.startActivity("SearchList", i  
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
.getDecorView();  

// Again, replace the view  
SearchActivityGroup.group.replaceView(view);
ActivityGroup:

package nl.dante.SuperDeals;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SearchActivityGroup extends ActivityGroup {

View rootView;

// Keep this in a static variable to make it accessible for all the nested
// activities, lets them manipulate the view
public static SearchActivityGroup 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);
    /*
     * this.history = new ArrayList<View>(); group = this;
     * 
     * // Start the root activity within the group and get its view View
     * view = getLocalActivityManager().startActivity("Search", new
     * Intent(this,Search.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
     * .getDecorView();
     * 
     * // Replace the view of this ActivityGroup replaceView(view);
     */

}

@Override
protected void onResume() {

    super.onResume();
    this.history = new ArrayList<View>();
    group = this;

    // Start the root activity within the group and get its view
    View view = getLocalActivityManager().startActivity("Search", new Intent(this, Search.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();

    // Replace the view of this ActivityGroup
    replaceView(view);
}

public void replaceView(View v) {
    // Adds the old one to history
    if (history.size() == 0) {
        if (rootView != null) {
            history.add(rootView);
            rootView = null;
        }
    }
    history.add(v);
    // Changes this Groups View to the new View.
    setContentView(v);
}

public void back() {
    try {
        if (history.size() > 0) {
            if (history.size() == 1) {
                rootView = history.get(0);
                Toasts.ToastImageView(this, "Druk nogmaals BACK om af te sluiten", R.drawable.power_64_off, "red");
            }
            history.remove(history.size() - 1);
            setContentView(history.get(history.size() - 1));
        } else {
            finish();
        }
        if (history.size() < 3) {
            // Tabhost.bannerImage2.setImageResource(0);
            Tabhost.banner.setBackgroundResource(R.drawable.gradient_blue);
        }
        if (history.size() == 2) {
            Tabhost.bannerImage1.setImageResource(R.drawable.sorteer_btn);
        }
    } catch (Exception ex) {
    }
}

public int getHistorySize() {
    return history.size();
}

@Override
public void onBackPressed() {
    try {
        SearchActivityGroup.group.back();
    } catch (Exception ex) {

    }
    return;
}
}
于 2013-02-06T10:31:40.180 に答える