Question

How do I realize options and context menus in a "clean" and proper way, when an Activity switches between several fragments?

Here example code using a NavigationDrawer:

public class MainActivity extends Activity implements OnFragmentInteractionListener {

    ...

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        FragmentManager fragmentManager = getFragmentManager();

        switch (position) {
        case 0:
            fragmentManager.beginTransaction().replace(R.id.container, Fragment1.newInstance(null, null)).commit();
            break;
        case 1:
            fragmentManager.beginTransaction().replace(R.id.container, Fragment2.newInstance(null, null)).commit();
            break;
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        // ???

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {            
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }

        // ???

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        // ???

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

        //???

        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {

        // ???

        return super.onContextItemSelected(item);
    }

    @Override
    public void onFragmentInteraction(String id) {
        // ???
    }
}

public class Fragment1 extends Fragment {

    // ???
    // option menu item A
    // listView1 context menu item X
}

public class Fragment2 extends Fragment {

    // ???
    // option menu item B
    // listView 2 context menu item Y
}

Maybe I am on the wrong track. I am a bit confused...

Was it helpful?

Solution

I understand you want to have different menus for each fragment? You can specify a menu for each fragment and it will only show when that fragment is attached to the Activity. The code below all belongs in a fragment.

You must tell the fragment it has an options menu:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

Then inflate the the menu for this fragment:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.about, menu);
}

Then handle each item in the menu when it is clicked:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle menu item click here
    return super.onOptionsItemSelected(item);
}

You can also specify a "global" menu in the activity and those menu item will be shown as well as the menu items for the current fragment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top