9

I have one Fragment:

ProductsFragments extends Fragment

and one Activity

AdminMenuActivity extends ActionBarActivity

I want to call ProductsFragments from AdminMenuActivity. I have used 2 options:

1)

FragmentManager fm = getSupportFragmentManager();
                for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
                    fm.popBackStack();
                }
                FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                tx.replace(R.id.frame_layout, android.support.v4.app.Fragment.instantiate(AdminMenuActivity.this, fragments[1]));
                tx.commit();

2)

Intent intent1 = new Intent(AdminMenuActivity.this, ProductsActivity.class);
                startActivity(intent1);

Both are failed. I don't want to extend ProductsFragments with FragmentActivity because it doesn't give me supportedActionBar v7

So how do I call Fragment?

mmBs
  • 8,421
  • 6
  • 38
  • 46
Rudra
  • 241
  • 1
  • 3
  • 9

2 Answers2

8

Here is how you call a fragment from inside an Activity

Fragment fr = new FirstFragment();
fr.setArguments(args);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();

Assuming you have fragment_place represents the following:

<fragment android:name="com.company.appName.fragments.FirstFragment"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
Dibyendu Mitra Roy
  • 1,604
  • 22
  • 20
0

If you are getting

Wrong 2nd argument type. Found: 'android.support.v4.app.Fragment', required: 'android.app.Fragment'

this error, check the imports, you may find the below,

import android.app.FragmentManager;
import android.app.FragmentTransaction;

delete the above imports. Change getFragmentManager()to getSupportFragmentManager()

FragmentManager fragmentManager=getFragmentManager(); to
FragmentManager fragmentManager=getSupportFragmentManager();

now we can import the,

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
sandy
  • 1
  • 2