0

我在我的应用程序中使用标签。标签工作正常。一旦我点击选项卡,特定的活动就会出现在选项卡下。其中一个选项卡具有菜单功能,当我单击菜单选项卡时,菜单列表出现在选项卡下方。当我单击菜单列表时,我使用 View 在选项卡下显示活动。问题从这里开始,当我单击菜单列表并且活动出现后,我无法使用手机中的后退按钮。当我单击后退按钮时,它会杀死应用程序而不是返回菜单列表。下面是我的代码。

选项卡活动

public class NfcSurveyActivity extends TabActivity {


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
                mTechLists);

        int profileCount = db.getContactsCount();
        if (profileCount <= 0) {
            Intent intent = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int profileCount = db.getContactsCount();
        if (profileCount <= 0) {
            Intent intent1 = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(intent1);
        }

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            tabToDisplay = extras.getString("tab");
            if (tabToDisplay.equals("CAMERA")) {
                barcodeData = extras.getString("barcodeData");
            }
            extras.clear();
        }

        TabHost tabHost = getTabHost();

        // Home
        TabSpec tbspecHome = tabHost.newTabSpec("Home");
        tbspecHome.setIndicator("",
                getResources().getDrawable(R.drawable.tab_account_style));

        Intent iHome = new Intent(this, HomeActivity.class);
        tbspecHome.setContent(iHome);
        tabHost.addTab(tbspecHome);

        // History
        tabHost.addTab(tabHost
                .newTabSpec("Fun")
                .setIndicator("",
                        getResources().getDrawable(R.drawable.tab_fun_style))
                .setContent(
                        new Intent(this, NfcSurveyActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        if (tabToDisplay != null && tabToDisplay.equals("REDEEM")) {
            if (barcodeData != null && barcodeData.length() > 0) {

                tabHost.addTab(tabHost
                        .newTabSpec("Camera")
                        .setIndicator(
                                "",
                                getResources().getDrawable(
                                        R.drawable.tab_redeem_style))
                        .setContent(
                                new Intent(this, NfcSurveyActivity.class)
                                        .addFlags(
                                                Intent.FLAG_ACTIVITY_CLEAR_TOP)
                                        .putExtra("autoLoadBarcodeData",
                                                barcodeData)));

            }

            else {
                tabHost.addTab(tabHost
                        .newTabSpec("Camera")
                        .setIndicator(
                                "",
                                getResources().getDrawable(
                                        R.drawable.tab_redeem_style))
                        .setContent(
                                new Intent(this, NfcSurveyActivity.class)
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

            }

        } else {
            tabHost.addTab(tabHost
                    .newTabSpec("Camera")
                    .setIndicator(
                            "",
                            getResources().getDrawable(
                                    R.drawable.tab_redeem_style))
                    .setContent(
                            new Intent(this, NfcSurveyActivity.class)
                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        }

        // tabHost.setCurrentTab(2);

        tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = tabHost
                .getTabWidget().getChildAt(2).getLayoutParams().height + 19;

        // Search
        TabSpec tbspecSearch = tabHost.newTabSpec("Finder");
        tbspecSearch.setIndicator("",
                getResources().getDrawable(R.drawable.tab_finder_style));

        Intent iSearch = new Intent(this, NfcSurveyActivity.class);
        tbspecSearch.setContent(iSearch);
        tabHost.addTab(tbspecSearch);

        // Profile
        TabSpec tbspecProfile = tabHost.newTabSpec("Quit");
        tbspecProfile.setIndicator("",
                getResources().getDrawable(R.drawable.tab_quit_style));

        Intent iProfile = new Intent(this, NfcSurveyActivity.class);
        tbspecProfile.setContent(iProfile);

        tabHost.addTab(tbspecProfile);

        for (int i = 0; i <= 4; i++) {
            tabHost.getTabWidget()
                    .getChildTabViewAt(i)
                    .setBackgroundColor(
                            getResources()
                                    .getColor(android.R.color.transparent));

            if (i == 2) {
                tabHost.getTabWidget()
                        .getChildTabViewAt(i)
                        .setPadding(
                                tabHost.getTabWidget().getChildTabViewAt(i)
                                        .getPaddingLeft(),
                                tabHost.getTabWidget().getChildTabViewAt(i)
                                        .getPaddingTop(),
                                tabHost.getTabWidget().getChildTabViewAt(i)
                                        .getPaddingRight(), 20);
            }

        }

        if (tabToDisplay != null && tabToDisplay.length() > 0) {
            if (tabToDisplay.equals("CAMERA")) {
                tabHost.setCurrentTab(2);

            } else if (tabToDisplay.equals("HISTORY")) {
                tabHost.setCurrentTab(1);

            }

        }

        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            public void onTabChanged(String tabId) {
                NfcSurveyConfiguration.SelectedTab = tabId;
            }
        });
    }

}

菜单活动

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_item_row);

initList();

// We get the ListView component from the layout
ListView lv = (ListView) findViewById(R.id.listView);
simpleAdpt = new SimpleAdapter(this, planetsList,
        android.R.layout.simple_list_item_1, new String[] { "planet" },
        new int[] { android.R.id.text1 });

lv.setAdapter(simpleAdpt);
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> parentAdapter, View view,
            int position, long id) {

        TextView clickedView = (TextView) view;
        String menu = clickedView.getText().toString();
        if (menu.equalsIgnoreCase("Point History")) {
            View view1 = getLocalActivityManager().startActivity(
                    "ReferenceName",
                    new Intent(getApplicationContext(),
                            PointsHistory.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                    .getDecorView();
            setContentView(view1);
        }
        else
        {
            View view1 = getLocalActivityManager().startActivity(
                    "ReferenceName",
                    new Intent(getApplicationContext(),
                            LoginActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                    .getDecorView();
            setContentView(view1);
        }

    }
});

}
4

3 回答 3

0

您可以尝试添加 onkeydown 方法,然后尝试做您想做的事情

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    //Handle the back button
    if( keyCode == KeyEvent.KEYCODE_BACK && isTaskRoot()) 
    {
//ur code here to do the required step either to exit the app or stay in there
}

return true;
}
于 2013-03-22T08:15:58.117 回答
0

尝试使用包含您调用的活动列表的 ActivityGroup,您需要 在 TabActivity 中处理多个活动。另请阅读选项卡组活动

或者

您可以覆盖后退按钮并完成活动维护一个打开的活动列表,然后在背面按下使用开始活动打开活动,当列表结束时完成您的选项卡活动

也读过这个这个链接

于 2013-03-22T08:49:59.113 回答
0

你可以用这种方式

public void onBackPressed() {

    };

当活动检测到用户按下返回键时调用。默认实现只是完成当前活动,但您可以覆盖它来做任何您想做的事情。

检查这个:https ://stackoverflow.com/a/12171478/1168654

于 2013-03-22T09:26:21.893 回答