Alexis
Alexis

Reputation: 73

android menu item open new fragment

i'm using swiping tabs as navigation to display 5 pages ( fragments) , in the app menu ( 3 dot ) button i want to create 2 pages, 1 for settings, and other for about page ( just static text) . please advice how can i to this.

in specific, how can i go to a new fragment ( not in my tab) to be displayed when clicked from menu items.

please assist me, thank you in advance.

this is my code so far.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){

        case R.id.action_reset:

            Unsafe.uhs1a.setSelection(0);
            Unsafe.uhs1b.setSelection(0);
            Unsafe.uhs1c.setSelection(0);
            Precondition.phs1a.setSelection(0);
            Precondition.phs1b.setSelection(0);
            Precondition.phs1c.setSelection(0);

        case R.id.action_about:

            // need to open a static page ( fragment) here 

        return true;
        default:
            return super.onOptionsItemSelected(item);
    }


}

}

Upvotes: 0

Views: 3947

Answers (2)

Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27237

Old question but WTH.You do this using a FragmentManager and her friend FragmentTransaction:

@Override
public boolean onOptionsItemSelected(MenuItem item){
   switch (item.getItemId()){

    case R.id.action_reset:

        Unsafe.uhs1a.setSelection(0);
        Unsafe.uhs1b.setSelection(0);
        Unsafe.uhs1c.setSelection(0);
        Precondition.phs1a.setSelection(0);
        Precondition.phs1b.setSelection(0);
        Precondition.phs1c.setSelection(0);

    case R.id.action_about:
      Fragment newFragment = new TheFragmentYouWantToOpen();
      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.frame_container, newFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
    //frame_container is the id of the container for the fragment
    return true;
    default:
        return super.onOptionsItemSelected(item);
}

Upvotes: 1

zozelfelfo
zozelfelfo

Reputation: 3776

Not sure if this is what you want, but I have a fragment which opens a new Actvity (with a fragment inside):

 public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.action_about:
         Intent intent_to = new Intent(getActivity(),MyNewActivity.class);
            startActivity(intent_to);
            break;

    }
    return true;
}

Inside MyNewActivity class you should have a container and open a fragment using getFragmentManager()

You can even use startActivityForResult if you want to do something depending in how the new fragment closes

Hope it helps

Upvotes: 0

Related Questions