2011-08-16 37 views
0

我的問題是切換標籤使用活動組它要顯示上次活動。 我想顯示最後打開/訪問屏幕,當我們瀏覽tab.My一個是去第一個畫面:切換標籤使用活動組它想要顯示上次活動

這是我maninActivity」

public class MainActivity extends TabActivity { 
    int selectedTab; 
    TabHost tabHost ; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabview); 

     TabHost t = getTabHost(); 
     tabHost = (TabHost)findViewById(android.R.id.tabhost); 
     Resources res = getResources(); 
     TabSpec firstTabSpec = tabHost.newTabSpec("tid1"); 
     TabSpec secondTabSpec = tabHost.newTabSpec("tid1"); 
     TabSpec thirdTabSpec = tabHost.newTabSpec("tid1"); 
     TabSpec fouthTabSpec = tabHost.newTabSpec("tid1"); 
     /** TabSpec setIndicator() is used to set name for the tab. */ 
     /** TabSpec setContent() is used to set content for a particular tab. */ 
     firstTabSpec.setIndicator("Sales",res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(new Intent(this,SalesActivityGroup.class)); 
     secondTabSpec.setIndicator("Admin",res.getDrawable(R.drawable.admin)).setContent(new Intent(this,SettingActivityGroup.class)); 
     thirdTabSpec.setIndicator("Setting",res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(new Intent(this,SettingActivityGroup.class)); 
     fouthTabSpec.setIndicator("Inquiry",res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(new Intent(this,SettingActivityGroup.class)); 

     tabHost.addTab(firstTabSpec); 
     tabHost.addTab(secondTabSpec); 
     tabHost.addTab(thirdTabSpec); 
     tabHost.addTab(fouthTabSpec); 
     tabHost.setCurrentTab(0); 
     tabHost.setMinimumHeight(18); 
     tabHost.setFadingEdgeLength(5); 

    } 

    public void onTabChanged(String arg0) { 
      selectedTab = tabHost.getCurrentTab(); 

    } 
} 

這是我SalesActivityGroup

public class SalesActivityGroup extends ActivityGroup { 

    public static SalesActivityGroup group; 
    private ArrayList<View> history; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.history = new ArrayList<View>(); 
     group = this; 

     View view = getLocalActivityManager().startActivity("Sales", 
       new Intent(this, SalesRouteActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
       .getDecorView(); 

     replaceView(view); 

    } 

    public void replaceView(View v) { 
     history.add(v); 
     setContentView(v); 

    } 

    public void back() { 
     if (history.size() > 0) { 
      history.remove(history.size() - 1); 
      if (history.size() > 0) { 
       setContentView(history.get(history.size() - 1)); 
      } else { 
       finish(); 
      } 
     } else { 
      finish(); 
     } 
    } 

    public void backToFirst() { 
     int size = history.size(); 
     while (size > 1) { 
      history.remove(size - 1); 
      size = history.size(); 
     } 
     setContentView(history.get(0)); 
    } 

    @Override 
    public void onBackPressed() { 
     SalesActivityGroup.group.back(); 
     return; 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // super.onActivityResult(requestCode, resultCode, data); 
     Log.i("****" , "requestCode" + requestCode); 
     Bundle bundle = data.getExtras(); 
     String roteCode = bundle.getString("RouteCode"); 
     Intent intent = new Intent(SalesActivityGroup.this,ListRetailerActivity.class); 
     bundle.putString("RouteCode", roteCode); 
     intent.putExtras(bundle); 
     View view = SalesActivityGroup.group.getLocalActivityManager() 
       .startActivity("",intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
     SalesActivityGroup.group.replaceView(view); 
     } 


} 

這是我在SalesRouteActivity的電話部分

Intent intent = new Intent(SalesRouteActivity.this, ListRetailerActivity.class); 
         Bundle bundle = new Bundle(); 
         bundle.putString("RouteName", keyword); 
         intent.putExtras(bundle); 
      View view = SalesActivityGroup.group.getLocalActivityManager().startActivity("", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
      SalesActivityGroup.group.replaceView(view); 

S我喜歡上面的代碼,我有設置ActivityGroup爲下一個標籤

public class SettingActivityGroup extends ActivityGroup { 

    // Keep this in a static variable to make it accessible for all the nested 
    // activities, lets them manipulate the view 
    public static SettingActivityGroup 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 withing the group and get its view 
     View view = getLocalActivityManager().startActivity(
       "Setting", 
       new Intent(this, SettingScreenActivity.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 
     history.add(v); 
     // Changes this Groups View to the new View. 
     setContentView(v); 

    } 


    public void back() { 
     if (history.size() > 0) { 
      history.remove(history.size() - 1); 
      if (history.size() > 0) { 
       setContentView(history.get(history.size() - 1)); 
      } else { 
       finish(); 
      } 
     } else { 
      finish(); 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     SettingActivityGroup.group.back(); 
     return; 
    } 

} 

我粘貼我的代碼在這裏。 http://pastebin.com/D4fvkGBx

我面臨這樣的麻煩......

請幫我

哪裏是錯誤的,我的代碼?

在此先感謝

+0

的ActivityGroup已過時,使用新的片段和FragmentManager的API來代替。 – pgsandstrom

+0

請幫我解決這個問題 – Piraba

回答

0

有我的MainActivity類別

tabHost = getTabHost(); 
    TabHost.TabSpec spec; 
    Intent intent; 

    intent = new Intent().setClass(this, SalesActivityGroup.class); 
    spec = getTabHost().newTabSpec("Sales").setIndicator("Sales",getResources().getDrawable(R.drawable.sales)).setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, SettingActivityGroup.class); 
    spec = getTabHost().newTabSpec("Admin").setIndicator("Admin",getResources().getDrawable(R.drawable.admin1)).setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, SettingActivityGroup.class); 
    spec = getTabHost().newTabSpec("Setting").setIndicator("Setting",getResources().getDrawable(R.drawable.maintenance)).setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, SettingActivityGroup.class); 
    spec = getTabHost().newTabSpec("Inquiry").setIndicator("Inquiry",getResources().getDrawable(R.drawable.inquiry)).setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.selected_tab); 


    tabHost.setCurrentTab(0); 
    tabHost.setMinimumHeight(18); 
    tabHost.setFadingEdgeLength(5); 
    tabHost.setFocusable(true); 
    tabHost.requestFocus(); 
    tabHost.setFadingEdgeLength(5); 

沒有必要在這裏呼籲findById()的一些問題。

這是錯誤的tabHost = (TabHost)findViewById(android.R.id.tabhost);

0

你需要重寫

onBackPressed 

並設置要通過處理後按下按鈕做什麼。例如,您可以在用戶按下後顯示以前的選項卡或您想要的某個默認選項卡。

在創建保存選項卡中的metoth
+0

我覆蓋了'@Override public void onBackPressed(){ SalesActivityGroup.group.back(); return; } '和'回來()'我做到了。我沒有工作,請幫助我。請多解釋一下 – Piraba

0

您使用:)

int lastTab; 
@Override public void onBackPressed() { 
     tabHost.setCurrentTab(lastTab); 
     tabHost.setMinimumHeight(18); 
     tabHost.setFadingEdgeLength(5);return; }