0

I have Main activity:

public class GeneralActivity extends ActionBarActivity {
...
}

I have another class in which I create a drawer, I bring to the required parameters. But when I want to move to a different fragment when you click on the menu item, then IDE writes that there is no method getSupportFragmentManager ().

public class DrawerClass {

    public static void drawer(final Activity activity, Toolbar toolbar) {
    ...
    result.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
            @Override
            public boolean onItemClick(View view, int i, IDrawerItem iDrawerItem) {
                Log.d("POSITION", "position = " + i);
                switch (i) {
                ....
                    case 5:
                        Toast.makeText(activity, R.string.drawer_item_journal, Toast.LENGTH_SHORT).show();
                        JournalFragment journalFragment = new JournalFragment();
                        FragmentTransaction transaction = getSupportFragmentManager()
                                .beginTransaction();
                        transaction.replace(R.id.profile_fragment_layout, journalFragment);
                        transaction.commit();
                        break;
                }
                return false;
            }
        });

I have an error on the line FragmentTransaction transaction = getSupportFragmentManager() And I don't know how fix it.
I use this drawer in several classes, so I created a separate class in order to avoid duplication of code

metalink
  • 594
  • 2
  • 8
  • 21

1 Answers1

0

you cant use getSupportFragmentManager outside of Activity or Fragment.. so do

activity.getSupportFragmentManager();

plus an advice to you change it from Static to Singleton..

Itzik Samara
  • 2,278
  • 1
  • 14
  • 18