Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include the activity in fragment tabs ?

I am developing an app in which there are two fragmenttabs.when pressing the tabs corresponding fragments will appear.that works fine.but what I want an activity inside the fragmenttabs. I am using ABS library for this.

ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.Tab tab1 = bar.newTab();
        ActionBar.Tab tab2 = bar.newTab();
tab1.setText("Fragment A");
        tab2.setText("Fragment B");
tab1.setTabListener(new MyTabListener<FragmentA>(this, "tab1",
                FragmentA.class, null));
        tab2.setTabListener(new MyTabListener<FragmentB>(this, "tab1",
                FragmentB.class, null));
        }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go Location selection
            Intent intent = new Intent(FragmentDemoActivity.this,
                    TestActivity.class);
             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             startActivity(intent);
             return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

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

listnerclass is

public class MyTabListener<T extends Fragment> implements ActionBar.TabListener {
    private final FragmentActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private final Bundle mArgs;
    private Fragment mFragment;

    public MyTabListener(FragmentActivity activity, String tag, Class<T> clz,
            Bundle args) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;

        FragmentTransaction ft = mActivity.getSupportFragmentManager()
                .beginTransaction();
        mFragment = mActivity.getSupportFragmentManager().findFragmentByTag(
                mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            ft.detach(mFragment);
        }
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft = mActivity.getSupportFragmentManager().beginTransaction();

        if (mFragment == null) {
            mFragment = Fragment
                    .instantiate(mActivity, mClass.getName(), mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
            ft.commit();
        } else {
            ft.attach(mFragment);
            ft.commit();
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft = mActivity.getSupportFragmentManager().beginTransaction();

        if (mFragment != null) {
            ft.detach(mFragment);
            ft.commitAllowingStateLoss();
        }

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

Fragmentclass

public class FragmentB extends Fragment {
    Button button;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
    {
        return inflater.inflate(R.layout.frag_b, group, false);
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        button = (Button) getActivity().findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Fragment B");
                builder.setMessage("What would you like to do?");
                builder.setPositiveButton("Nothing", null);
                builder.setNegativeButton("Leave me alone!", null);
                builder.show();
            }
        });
    }
}

i wnt to include the following activity in the fragmentB

public class TestActivity extends Activity {

    Button b1, b2;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testactivity);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);

        tv = (TextView) findViewById(R.id.textView1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tv.setText("You Clicked on Button 1");
            }
        });
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tv.setText("You Clicked on Button 2");
            }
        });

    }
}

I have lot of created activites to include in the fragment..its un imaginable to recreate in onActivityCreated of fragment class. according to this I have to modify my main application.

like image 910
Noufal Avatar asked Mar 21 '23 17:03

Noufal


1 Answers

A Fragment can't host an activity. Instead of activity, you can use Nested Fragment. A simple tutorial: http://xperiment-andro.blogspot.com/2013/02/nested-fragments.html

like image 135
Fahim Ahmed Avatar answered Mar 28 '23 13:03

Fahim Ahmed