Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

android - Handle back press in tabs with fragment

I am using FragmentTabHost in my app. I have three tabs. Each tab shows a Fragment.

addTab("Tab1", R.drawable.ic_launcher, Fragment1.class);
addTab("Tab2", R.drawable.ic_launcher, Fragment2.class);
addTab("Tab3", R.drawable.ic_launcher, Fragment3.class);
addTab("Tab3", R.drawable.ic_launcher, Fragment4.class);

When I press back from any of these tabs, the app is closed and home screen is shown. Now what I want is, when I press back from Tab1, the app should close. However, if I press back from Tab2 or Tab3, the user should be sent to Tab1. Summary is:

Currently in Tab1 -> press back -> app close

Currently in Tab2 -> press back -> Go to Tab1

Currently in Tab3 -> press back -> Go to Tab1

How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your Activity (where you have your FragmentTabHost) override the onBackPressed(). In onBackPressed() you can check for the currentTab's position.

If current tab is not 0 (i.e. not the first tab) then set the previous tab as the current tab.

Else if current tab is 0, exit the app.

Since i do not have your actual class, I created a dummy class with FragmentTabHost just to demonstrate how it can be done.

public class MainActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mTabHost = (FragmentTabHost) findViewById(R.id.tab_host);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.tab_framelayout);

        mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator("Tab1",
                        getResources().getDrawable(R.drawable.ic_launcher)),
                Fragment1.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator("Tab2",
                        getResources().getDrawable(R.drawable.ic_launcher)),
                Fragment2.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab3").setIndicator("Tab3",
                        getResources().getDrawable(R.drawable.ic_launcher)),
                Fragment3.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab4").setIndicator("Tab4",
                        getResources().getDrawable(R.drawable.ic_launcher)),
                Fragment4.class, null);
    }

    @Override
    public void onBackPressed() {
       
        //get current tab index.
        int index = mTabHost.getCurrentTab();

        //decide what to do
        if(index!=0){
            mTabHost.setCurrentTab(index-1);
        } else {
            super.onBackPressed();
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...