-1

I used following segment to replace FrameLayout with Fragment inside onNavigationItemSelected method,

public boolean onNavigationItemSelected(MenuItem item) {
     int id = item.getItemId();
     Fragment fragment;

     if (id == R.id.nav_camara) {
         fragment = new MyFragment();
         FragmentTransaction ft = getFragmentManager().beginTransaction();
         ft.replace(R.id.mainFrame, fragment);
         ft.commit();
     }
     else if (id == R.id.nav_gallery) {

     }
     else if (id == R.id.nav_slideshow) {

     }
     else if (id == R.id.nav_manage) {

     } else if (id == R.id.nav_share) {

     } else if (id == R.id.nav_send) {

     }
}

I wanted to replace FrameLayout with FragmentActivity, then I did

FragmentActivity fragmentAc;

 if (id == R.id.nav_camara) {
     fragmentAc= new MyFragmentActivity();
     FragmentTransaction ft = getFragmentManager().beginTransaction();
     ft.replace(R.id.mainFrame, fragmentAc);
     ft.commit();
 }

but getting Wrong 2nd argument type. Found: 'android.support.v4.app.FragmentActivity', required: 'android.app.Fragment' on line ft.replace(R.id.mainFrame, fragmentAc);

What should I change now, Help me

2
  • google.co.in/… Sep 1, 2017 at 19:43
  • @GowthamanM I wanted to open FragmentActivity not Fragment so please read question
    – user4497065
    Sep 1, 2017 at 19:52

1 Answer 1

0

You are trying to pass FragmentActivity where it requires Fragment, that's what error says.

If you want to open another activity rather than replacing fragment, you have to call startActivity(...)

replace:

if (id == R.id.nav_camara) {
     fragmentAc= new MyFragmentActivity();
     FragmentTransaction ft = getFragmentManager().beginTransaction();
     ft.replace(R.id.mainFrame, fragmentAc);
     ft.commit();
 }

with:

if (id == R.id.nav_camara) {
    startActivity(new Intent(getActivity(), MyFragmentActivity.class));
}
3
  • I want to replace FrameLayout with MyFragmentActivity
    – user4497065
    Sep 1, 2017 at 20:01
  • that's not possible you can't open an activity within another activity. you will have to add one more in back stack and so there is only possible way to do it by using startActivity Sep 1, 2017 at 20:04
  • For clarification/understanding read Fragment & Activity documentation from Android. Sep 1, 2017 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.