其他分享
首页 > 其他分享> > android-后退按钮不适用于TabActivity和Group Activity

android-后退按钮不适用于TabActivity和Group Activity

作者:互联网

我在tabactivity和group activity中有一个关于后退按钮的问题.

现在我有一个4标签,在标签中我还有一个活动.
后退按钮不适用于群组活动和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, StartActivity.class);
    spec = tabHost.newTabSpec("ReweHaupt").setIndicator("ReweHaupt",
      res.getDrawable(R.drawable.home))

                  .setContent(intent);
    tabHost.addTab(spec);

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

    intent = new Intent().setClass(this,FilialesuchenActivitiy.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("Notizblock").setIndicator("Notizblock",res.getDrawable(R.drawable.einkaufliste))
                  .setContent(intent);
    tabHost.addTab(spec);






}

EinkauflisteACtivity.java

 public class EinkauflisteActivity extends ActivityGroup {

// das brauchen wir für groupactivity

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.einkaufliste);
    this.history = new ArrayList<View>();
    group = this;

    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 = getLocalActivityManager().startActivity(
                    "Systemhaus",
                    new Intent(v.getContext(), SuchenActivitiy.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();
    }
}

public void onBackPressed() {
    back();
}

public void onClick(View v) {
    // TODO Auto-generated method stub

}

解决方法:

后退按钮在这里不起作用

你必须在按下时覆盖
每个活动中的onBackPressed()并维护一个堆栈并弹出所需的活动

标签:tabactivity,android
来源: https://codeday.me/bug/20191202/2084993.html