Amarnath Baitha
Amarnath Baitha

Reputation: 549

how can I call fragment from activity in android

how to call Fragment from Activity. I am using below code.here MainFragment extends Fragment only.

  MainFragment  mainFragment = new MainFragment();
                 getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, mainFragment).commit();

but "getSupportFragmentManager()" in my Activity class is not supported.

Upvotes: 1

Views: 13799

Answers (2)

Nouman Bhatti
Nouman Bhatti

Reputation: 1847

MainFragment mainFragment = (MainFragment)getFragmentManager().findFragmentById(R.id.fragmentBId);
mainFragment.performSomeTask();

Upvotes: 0

Nathua
Nathua

Reputation: 8836

You need extend your activity from FragmentActivity

Upvotes: 3

Related Questions