2013-02-06 45 views
1

我有一個TabHost活動與2個選項卡。另外我的應用程序基本上有2種不同類型的通知。現在我想通過點擊通知1和標籤2打開標籤1並點擊通知2.因此,我通過一個代表標籤ID的ID向通知目標添加了一個額外的內容。要改變Tabs,我嘗試使用getIntent()。getExtra(...)獲取TabHost Activity的onResume方法中的ID並更改Tab。該解決方案在大多數情況下都適用但它有時不能正常工作,但我不知道爲什麼。任何線索爲什麼它有時會起作用,有時不會?或者是否有更好的解決方案來解決這個問題?改變標籤意圖從通知

回答

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已被棄用。

這是我實現的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; 
} 
} 
+0

是不是有更簡單的方法?當我收到新的意圖時,爲什麼我不能更改標籤? – antumin