2011-12-15 69 views
0

我有一個tabactivity和grouptactivty中的後退按鈕的問題。 我有4個選項卡,在選項卡中我也有更多3個活動。當我打電話給一個活動時,這是沒有問題的。但如果我點擊後退按鈕,比我的應用程序接近完成。TabActivity GroupActivity返回按鈕不工作

這是我的代碼。我的福爾特在哪裏?

Tabactivity

public class TabbarActivity extends TabActivity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tabbar); 



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

    intent = new Intent().setClass(this, HauptmenuActivity.class); 
    spec = tabHost.newTabSpec("ReweHaupt") 
      .setIndicator("ReweHaupt", 
        res.getDrawable(R.drawable.home)) 
      .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, ContactGroupEinkaufliste.class); 
    spec = tabHost 
      .newTabSpec("EinkaufListe") 
      .setIndicator("EinkaufListe", 
        res.getDrawable(R.drawable.einkaufliste)) 
      .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, FilialesuchenActivity.class); 
    spec = tabHost 
      .newTabSpec("Filiarsuchen") 
      .setIndicator("Filiarsuchen", 
        res.getDrawable(R.drawable.searchbuton)) 
      .setContent(intent); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, NotizblockActivity.class); 
    spec = tabHost 
      .newTabSpec("Einkaufzettel") 
      .setIndicator("Einkaufzettel", 
        res.getDrawable(R.drawable.einkaufliste)) 
      .setContent(intent); 
    tabHost.addTab(spec); 

} 

ContactGroupEinkaufliste.java

public class ContactGroupEinkaufliste extends ActivityGroup { 

public static ContactGroupEinkaufliste groupContact; 


private ArrayList<View> history; 

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


    View view = getLocalActivityManager().startActivity(
      "idHoraireActivity", 
      new Intent(this, EinkauflisteActivity.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); 
     setContentView(history.get(history.size()-1)); 
    }else { 
     finish(); 
    } 
} 

// @Override 
public void onBackPressed() { 
    ContactGroupEinkaufliste.groupContact.back(); 
    return; 
}} 

EinkauflisteActivity.java

public class EinkauflisteActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.einkaufliste); 

    Button btnsenden = (Button) findViewById(R.id.btnsenden); 
    Button btnscaner = (Button) findViewById(R.id.btnscaner); 
    Button btnsuchen = (Button) findViewById(R.id.btnsuchen); 

    // wenn wir button clicken, rufen wir neu activity 

    btnsuchen.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      View view = ContactGroupEinkaufliste.groupContact 
        .getLocalActivityManager() 
        .startActivity(
          "suchenactivity", 
          new Intent(v.getContext(), SuchenActivity.class) 
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) 
        .getDecorView(); 
      ContactGroupEinkaufliste.groupContact.replaceView(view); 
     } 
    }); 

}} 

SuchenActivity.java

public class SuchenActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.suchen); 

}} 

謝謝

回答

0

而且你能指望什麼單擊後退按鈕時?默認操作將轉到上一個活動。你有TabActivity作爲主 - 所以你退出你的TabActivity。如果您想要以某種方式更改默認操作,請在TabActivity中覆蓋onBackPressed。但要小心 - 更改默認操作通常不利於用戶體驗。

+0

? 我可以寫tabbaractivity onbackpressed? – Lucky

0

試試這個在您的TabActivity ..

@Override 
public void onBackPressed() 
{ 
    // TODO Auto-generated method stub 
    Activity act = getCurrentActivity(); 
    act.onBackPressed(); 

    // super.onBackPressed(); 

} 
0

我發現這是不可能的覆蓋早在TabActivity按鈕操作。 你應該這樣做在主持活動

@Override 

     public void onBackPressed() { 

      Log.i("Kishu","[onBackPressed()] pressed"); 
      getParent().moveTaskToBack(true); 
      return; 

     } 

在這個例子中,按後退按鈕會把你的應用程序來備份。默認行爲將關閉它。

注意:你應該放棄你的TabActivity,而不是託管活動。