在后退按钮杀死Android应用程序

时间:2013-03-22 07:55:05

标签: android android-tabhost android-view back-button

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

TabActivity

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;
            }
        });
    }

}

MenuActivity

@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);
        }

    }
});

}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

尝试使用包含您调用的活动列表的ActivityGroup,您需要处理Multiple Activities in TabActivity。另请阅读tab group activity

OR

您可以覆盖后退按钮并完成活动,保留已打开的活动列表,并在后面按下使用开始活动打开活动以及列表结束时完成标签活动

阅读thisthis链接

答案 2 :(得分:0)

你可以这样使用

public void onBackPressed() {

    };

当活动检测到用户按下后退键时调用。默认实现只是完成当前活动,但您可以覆盖它以执行任何操作。

检查一下:https://stackoverflow.com/a/12171478/1168654