0

My problem is as follows:

I have one activity with many fragments. Main activity has "three dots" menu.

When I call a fragment, it has it's own menu (which is showing).

This fragment is a form with EditText fields, and checkboxes.

When a user changes something in those fields, and then press the "save" icon in the toolbar, the values get collected, and sent to the web service, and updated. BUT only the first time.

After going back (back arrow in toolbar), and returning to a fragment (new fragment gets created), after editing the fields, and pressing the "save" button, the form doesn't get saved/updated.

I debugged it, and found out that the "save" button which gets called for the second time, is actually the "save" button of the previous fragment.

The values in the editText fields gets collected inside a HashMap. And this HashMap gets updated and filled with new data inside new fragment. BUT when it gets to calling onOptionsItemSelected(), OLD DATA from the previous (or THE FIRST fragment) gets in the focus, and those data gets saved/updated.

So, my question is this: Is there a way to detach the menu items from the fragment, so when another (new) fragment is called, and gets created it attaches its own menu option to itself (not from the first fragment ever created)?

I tested it with System.identityHashCode(fragment), and when a user edit fields in "second" fragment, the hash code is OK, BUT when the "save" gets called hash code gets changed to the hash code of the first fragment.

This is the part of code from FormFragment:

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

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.menu_main, menu);
    int itemId = 0;
    for (ActionList menuItem : allData.getActionList()) {
        menu.add(1, itemId, itemId, menuItem.getPrompt());
        itemId++;
    }
    inflater.inflate(R.menu.menu_activity, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_favourite) {
        if (valuesArray != null) {
            ((CommInterface) getActivity()).getUpdateList(valuesArray, taskUrl, dataItemId);
            return true;
        } else {
            Toast.makeText(getActivity(), "Not updated!", Toast.LENGTH_SHORT).show();
            return true;
        }
    }
    for (ActionList menuName : allData.getActionList()) {
        if (item.getTitle().equals(menuName.getPrompt())) {
            ((CommInterface) getActivity()).formMenuItemSelected(menuName.getActionName(), position);
            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}

And the part of code from MainActivity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_choose_instance:
            ....
        case android.R.id.home:
            if (height > width) {
                getSupportFragmentManager().popBackStack();
                toolbarTitles.remove(toolbarTitlesCurrentNumber);
                toolbarTitlesCurrentNumber--;
                toolbar.setTitle(toolbarTitles.get(toolbarTitlesCurrentNumber));
                if (toolbarTitlesCurrentNumber == 0) {
                    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                    getSupportActionBar().setHomeButtonEnabled(false);
                }
            } else if (...) {
              .... 
            } 
        default:
            return super.onOptionsItemSelected(item);
    }
}
ravenns
  • 115
  • 1
  • 11

2 Answers2

0

Create the menu items under fragments not under activity, see the previous discussion Android Options Menu in Fragment

Community
  • 1
  • 1
Gaurav Tak
  • 70
  • 2
  • I've tried that, but I get the same result. Problem is that somehow the option for updating/saving in toolbar menu doesn't get destroyed when going back in backstack, and creating a new fragment object after that, and it looks like it's permanently attached to the first ever fragment created. – ravenns May 23 '16 at 12:41
0

The answer to this problem is: use the getChildFragmentManager() in TabbedPagerAdapter.

I was using the Activity's getSupportFragmentManager() which is wrong when you are using NESTED fragments like in TabbedFragment which has a ViewPager, and a ViewPager has (child) Fragments inside.

ravenns
  • 115
  • 1
  • 11