Search code examples
androidandroid-tabhostandroid-viewback-button

On back button kill android app


I'm using tab in my apps. The tabs are working fine. Once i click on the tab, particular activity appears under the tab. One of the tab has menu function and when i clicked on the menu tab, list of menu appears below the tab. When i clicked on the menu list, i used View to show activity under the tabs. The problems starts here, when i click on the menu list and once the activity appears, i cannot use back button in the phone. when i click the back button, it kill the apps instead go back to menu list. Below is my code.

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

    }
});

}

Solution

  • you can try to add the onkeydown method and then try to do what you want like

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