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);
}
}