1

我有一个带有 2 个选项卡的 TabHost Activity。此外,我的应用程序基本上有 2 种不同类型的通知。现在我想通过单击通知 1 打开选项卡 1,并通过单击通知 2 打开选项卡 2。所以我在通知意图中添加了一个额外的 ID,它代表选项卡的 ID。要更改选项卡,我尝试使用 getIntent().getExtra(...) 在 TabHost Activity 的 onResume 方法中获取 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

但是,请记住,在 ICS 中不推荐使用 ActivityGroups。

这是我对 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 回答