0

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

    }
});

}
chinna_82
  • 6,353
  • 17
  • 79
  • 134

3 Answers3

0

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;
}
krishna
  • 142
  • 7
  • 12
  • I put this code in my activity but when i debug and press the back key.. it doesn't go to this function at all.... – chinna_82 Mar 22 '13 at 08:29
  • if you have tried it correctly try adding it with @Override at the top of this function – krishna Mar 22 '13 at 09:47
0

Try using ActivityGroup which will contain the list of activities that you call, You need to handle Multiple Activities in TabActivity. Also read about tab group activity

OR

you can override the back button and insted of finishing the activity maintain a list of activities that are opened and on the back press open the activity using start activity and when the list ends finish your tab activity

Aslo read this and this links

Girish Nair
  • 5,148
  • 5
  • 40
  • 61
0

you can use this way

public void onBackPressed() {

    };

Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

check this : https://stackoverflow.com/a/12171478/1168654

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177