How to get which fragment has been selected

Dawood Awan picture Dawood Awan · Jun 20, 2013 · Viewed 12.2k times · Source

I am creating a tab application, using fragments.

I have successfully created tabs, but now I need to create an object when a specific fragment is selected. e.g. When I select SettingsTab I need to call a function that is defined in Test class. and similarly destroy

// SettingsTab
package com.example.sensorspositioningn;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SettingsTab extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.settingstab, container, false);

    }
}

Here is the code in mainActivity:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        appContext = getApplicationContext();

        //ActionBar
        ActionBar actionbar = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab Tab1= actionbar.newTab().setText("Tab1");
        ActionBar.Tab Tab2= actionbar.newTab().setText("Tab2");
        ActionBar.Tab Tab3= actionbar.newTab().setText("Tab3");
        ActionBar.Tab SettingsTab = actionbar.newTab().setText("Settings");

        Fragment Tab1F= new Tab1();
        Fragment Tab2F= new Tab2();
        Fragment Tab3F= new Tab3();
        Fragment mSettingsFragment = new SettingsTab();

        Tab1.setTabListener(new MyTabsListener(Tab1F));
        Tab1.setTabListener(new MyTabsListener(Tab2F));
        Tab1.setTabListener(new MyTabsListener(Tab3F));
        SettingsTab.setTabListener(new MyTabsListener(mSettingsFragment));

        actionbar.addTab(Tab1);
        actionbar.addTab(Tab2);
        actionbar.addTab(Tab3);
        actionbar.addTab(SettingsTab);


    }
protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
    }

class MyTabsListener implements ActionBar.TabListener {
        public Fragment fragment;

        public MyTabsListener(Fragment fragment) {
            this.fragment = fragment;
        }


        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
        }


        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            ft.replace(R.id.fragment_container, fragment);




        }


        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(fragment);


        }
    }

Answer

jaibatrik picture jaibatrik · Sep 26, 2013

In your ActionBar.TabListener you can do this - (I'm assuming that you need the object to be in the Activity)

class MyTabsListener implements ActionBar.TabListener {
    public Fragment fragment;

    public MyTabsListener(Fragment fragment) {
        this.fragment = fragment;
    }


    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
    }


    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.replace(R.id.fragment_container, fragment);

        if(fragment instanceof SettingsTab) {
            // Create your object, call your function
        }
    }


    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);

        if(fragment instanceof SettingsTab) {
            // Destroy your object
        }
    }
}