2

I am making an application where i have set two bottom tabs,

namely "Exit" and "Back".

Now i want to exit from the application on the click of the "Exit" tab(not button).

How can i do that, i know that in android we can never actually close the application, but i want to go to the Home-screen on clicking "exit".

i have studied following link also along with other links

Is quitting an application frowned upon?

EDIT

public class Man_age_ur_PhoneActivity extends TabActivity {

    /** Called when the activity is first created. */
    ListView listview;


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

    private void setTabs()
    {
        addTab("Exit", R.drawable.tab_exit);
        addTab("Back", R.drawable.tab_back);
        //To add more tabs just use addTab() method here like previous line.
    }

    private void addTab(String labelId, int drawableId)
    {
        TabHost tabHost = getTabHost();
//      Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("" + labelId);    

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
//      spec.setContent(intent);
        tabHost.addTab(spec);
        }
}
Community
  • 1
  • 1
Beenal
  • 317
  • 1
  • 3
  • 14

3 Answers3

1

on Click of your second tab , you just have to write "finish();". This will close previously opened activities and you can exit from your application.

Harsh Trivedi
  • 1,012
  • 8
  • 25
0

you can use this code

public class Man_age_ur_PhoneActivity extends TabActivity {

    /** Called when the activity is first created. */
    ListView listview;


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

    getTabHost().setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String tabId) {

    int i = getTabHost().getCurrentTab();
     Log.i("@@@@@@@@ ANN CLICK TAB NUMBER", "------" + i);

        if (i == 0) {
                        Log.i("@@@@@@@@@@ Inside onClick tab 0", "onClick tab");

                        }
           else if (i ==1) {
                        Log.i("@@@@@@@@@@ Inside onClick tab 1", "onClick tab");
             }

                }
            });



    }

    private void setTabs()
    {
        addTab("Exit", R.drawable.tab_exit);
        addTab("Back", R.drawable.tab_back);
        //To add more tabs just use addTab() method here like previous line.
    }

    private void addTab(String labelId, int drawableId)
    {
        TabHost tabHost = getTabHost();
//      Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("" + labelId);    

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
//      spec.setContent(intent);
        tabHost.addTab(spec);
        }
}

Also import this

import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

Taken from this link Android TabWidget detect click on current tab

Community
  • 1
  • 1
Nitin
  • 1,966
  • 4
  • 22
  • 53
  • See the edited question, i want to add the tab change listener in here.How am i suppose to do that?? – Beenal Jan 07 '12 at 09:32
0

Generally Android apps shouldn't have an exit option, see for instance this post: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html

If you really want to, you can also just call System.exit(), this is guaranteed to close down the application completely.