我想为标签设置一个条件,当我按下该按钮时,所有操作都显示在“主页”选项卡上,但是当我按下该按钮时,所有活动结束。

这是我的代码。尝试帮助我。

 backpressed = true;

   if (results.equals("HOME")) {
        mTabHost.setCurrentTab(0);
        backpressed = false;
    } else if (results.equals("B")) {
        mTabHost.setCurrentTab(2);
    } else if (results.equals("C")) {
        mTabHost.setCurrentTab(3);
    } else if (results.equals("D")) {
        mTabHost.setCurrentTab(1);
    } else if (results.equals("E")) {

        singleton.openNewsFeed = true;
        mTabHost.setCurrentTab(4);
    } else {
        singleton.openMessage = true;
        mTabHost.setCurrentTab(4);
    }


这是我的onBackPressed

 @Override
public void onBackPressed() {
    /*super.onBackPressed();*/

    if (backpressed == true) {
        Intent intent = new Intent(getApplicationContext(), Dashboard.class);
        intent.putExtra("result", "HOME"); // getText() SHOULD NOT be static!!!
        startActivity(intent);
    }else{
        //FINISH
       super.onBackPressed();
    }

}

最佳答案

你这里情况不对

你应该尝试这个

 backpressed = false;

   if (results.equals("HOME")) {
        mTabHost.setCurrentTab(0);
        backpressed = true;
    } else if (results.equals("B")) {
        mTabHost.setCurrentTab(2);
    } else if (results.equals("C")) {
        mTabHost.setCurrentTab(3);
    } else if (results.equals("D")) {
        mTabHost.setCurrentTab(1);
    } else if (results.equals("E")) {

        singleton.openNewsFeed = true;
        mTabHost.setCurrentTab(4);
    } else {
        singleton.openMessage = true;
        mTabHost.setCurrentTab(4);
    }


背面按

 @Override
public void onBackPressed() {
    /*super.onBackPressed();*/

    if (backpressed == true) {
        Intent intent = new Intent(getApplicationContext(), Dashboard.class);
        intent.putExtra("result", "HOME"); // getText() SHOULD NOT be static!!!
        startActivity(intent);
    }else{
        //FINISH
       //super.onBackPressed();
       mTabHost.setCurrentTab(0);
    }

}

关于java - onBackPressed到主屏幕,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58369172/

10-14 10:23