Good evening everyone! I have two fragments, one calls the other and gets the result from it. In both fragments there are one and the same spiner that take their data from one singleton. When moving from the first fragment to the second, I transfer the id of the selected element from the first spiner, which is easily placed in the second. But the reverse situation does not work. If in the second fragment I change the value of the spiner, then the first one does not put it as such. Moreover, it is clear from the logs that onItemSelected is not called at all at the moment in which it should, or I miss something a lot. I bring the code and logs, I really hope for your help! Fragment1:

public class FragmentMainWindow extends Fragment implements View.OnClickListener { private static final int REQUEST_NEW_OPERATION = 0; private static final String LOG_CODE = "LOG_CODE"; private SpendingLab mSpendingLab; private PieGraph mPieGraph; private LinearLayout mPieChartContainer; private int mLastNumInDB = -1; private String[][] mDataBase; private Spinner mSpinnerAccount; private int mCurrentAccountId = 0; @Override public void onCreate (Bundle savedInstanceState){ super.onCreate(savedInstanceState); mDataBase = new String[100][3]; mSpendingLab = SpendingLab.getInstance(getActivity()); mPieGraph = new PieGraph(); Log.d(LOG_CODE, "FragmentMainWindow ### Opened main window fragment (onCreate now)."); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main_window, parent, false); Log.d(LOG_CODE, "FragmentMainWindow ### onCreateView"); //Making spinner for account mSpinnerAccount = (Spinner) v.findViewById(R.id.spinner_account_main); setAccountSpinner(mCurrentAccountId, mSpendingLab.getAccountsNames()); Log.d(LOG_CODE, "FragmentMainWindow ### currentAccountId is - " + Integer.toString(mCurrentAccountId)); mSpinnerAccount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Log.d(LOG_CODE, "FragmentMainWindow ### onItemSelected called"); if (id == mSpendingLab.getAccountsNames().size() - 1){ Log.d(LOG_CODE, "FragmentMainWindow ### User required creating new account dialog"); AlertDialog.Builder builderDialogNewAccount = new AlertDialog.Builder(getActivity()); final EditText editTextNewAccount = new EditText(getActivity()); builderDialogNewAccount.setTitle(getResources().getString(R.string.alert_dialog_new_account_title)); builderDialogNewAccount.setMessage(getResources().getString(R.string.alert_dialog_new_account_message)); builderDialogNewAccount.setView(editTextNewAccount); builderDialogNewAccount.setPositiveButton(getResources().getString(R.string.alert_dialog_button_create), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mSpendingLab.addAccountNameAtIndex(mSpendingLab.getAccountsNames().size() - 1, editTextNewAccount.getText().toString()); setAccountSpinner(mSpendingLab.getAccountsNames().size() - 2, mSpendingLab.getAccountsNames()); Log.d(LOG_CODE, "FragmentMainWindow ### User added new account"); } }); builderDialogNewAccount.setNegativeButton(getResources().getString(R.string.alert_dialog_button_cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mSpinnerAccount.setSelection(mCurrentAccountId); Log.d(LOG_CODE, "FragmentMainWindow ### User pressed cancel in new account dialog"); } }); builderDialogNewAccount.setCancelable(true); builderDialogNewAccount.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { mSpinnerAccount.setSelection(mCurrentAccountId); Log.d(LOG_CODE, "FragmentMainWindow ### User canceled new account dialog by pressing back button"); } }); builderDialogNewAccount.show(); } else if (mLastNumInDB != -1){ Log.d(LOG_CODE, "FragmentMainWindow ### User changed account on " + mSpinnerAccount.getSelectedItem() + ". currentAccountId is - " + Integer.toString(mCurrentAccountId) + ". Id in onItemSelected is - " + Long.toString(id)); createNewPieGraph(); } } @Override public void onNothingSelected(AdapterView<?> parent) { //nothing to do } }); mPieChartContainer = (LinearLayout) v.findViewById(R.id.linearLayout_pieChart_container); if (mLastNumInDB == -1){ //Initing empty PieGraph if we have no values GraphicalView graphicalView = mPieGraph.getEmptyGraphicalView(getActivity()); mPieChartContainer.addView(graphicalView); Log.d(LOG_CODE, "FragmentMainWindow ### Empty pieGraph was made"); } else{ Log.d(LOG_CODE, "FragmentMainWindow ### createNewPieGraph method was called"); createNewPieGraph(); } //Click on minus button ImageButton operationButtonMinus = (ImageButton) v.findViewById(R.id.operation_button_minus); operationButtonMinus.setOnClickListener(this); return v; } @Override public void onClick(View view){ switch (view.getId()){ case R.id.operation_button_minus: //Making Bundle with account and category for autofill in operation fragment Bundle args = new Bundle(); args.putInt("account", mSpinnerAccount.getSelectedItemPosition()); FragmentManager fm = getActivity().getSupportFragmentManager(); FragmentOperationWindow fragment = new FragmentOperationWindow(); fragment.setArguments(args); fragment.setTargetFragment(FragmentMainWindow.this, REQUEST_NEW_OPERATION); fm.beginTransaction().replace(R.id.fragment_main_window_container, fragment, "operation fragment opened").addToBackStack(null).commit(); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode, data); if (resultCode != Activity.RESULT_OK) return; if (requestCode == REQUEST_NEW_OPERATION){ String newSpending = (String) data.getSerializableExtra(FragmentOperationWindow.NEW_OPERATION_SPEND); int newSpendingAccountInt = (int) data.getSerializableExtra(FragmentOperationWindow.NEW_OPERATION_ACCOUNT); int newSpendingCategoryInt = (int) data.getSerializableExtra(FragmentOperationWindow.NEW_OPERATION_CATEGORY); mCurrentAccountId = newSpendingAccountInt; String newSpendingAccount = Integer.toString(newSpendingAccountInt); String newSpendingCategory = Integer.toString(newSpendingCategoryInt); mLastNumInDB++; mDataBase[mLastNumInDB][0] = newSpendingAccount; mDataBase[mLastNumInDB][1] = newSpendingCategory; mDataBase[mLastNumInDB][2] = newSpending; Log.d(LOG_CODE, "FragmentMainWindow ### Returned from operation fragment. Account: " + newSpendingAccount + ". Category: " + newSpendingCategory + ". Spending: " + newSpending); } } private void createNewPieGraph (){ int currentAccountId = mSpinnerAccount.getSelectedItemPosition(); Log.d(LOG_CODE, "FragmentMainWindow ### Current selected item in account spinner is - " + Integer.toString(currentAccountId)); ArrayList<Integer> currentCategorysList = new ArrayList<Integer>(); ArrayList<Double> currentSpendingsList = new ArrayList<Double>(); ArrayList<String> currentCategorysNameList = new ArrayList<String>(); ArrayList<Integer> currentCategoryColorsList = new ArrayList<Integer>(); //currentSpendingsList - is for spendings, currentCategorysList - is for category names, //currentCategorysNameList - is for category names, we'll use to create pieGraph //currentCategoryColorsList - is for colors, we'll do same as with currentCategorysNameList for (int i = 0; i <= mLastNumInDB; i++){ if (Integer.valueOf(mDataBase[i][0]) == currentAccountId){ if (!currentCategorysList.contains(mDataBase[i][1])){ currentCategorysList.add(Integer.valueOf(mDataBase[i][1])); currentSpendingsList.add(Double.valueOf(mDataBase[i][2])); } else{ int index = findValueInIntegerArrayList(Integer.valueOf(mDataBase[i][1]), currentCategorysList); if (index > -1){ double value = currentSpendingsList.get(index); currentSpendingsList.set(index, value + Double.valueOf(mDataBase[i][2])); } else{ Log.d(LOG_CODE, "FragmentMainWindow ### findValueInIntegerArrayList did not find value"); } } } } GraphicalView graphicalView; if (currentCategorysList.size() > 0){ for (int i = 0; i < currentCategorysList.size(); i++){ currentCategorysNameList.add(mSpendingLab.getCategory(currentCategorysList.get(i))); currentCategoryColorsList.add(mSpendingLab.getColor(currentCategorysList.get(i))); } graphicalView = mPieGraph.getGraphicalView(getActivity(), currentSpendingsList, currentCategorysNameList, currentCategoryColorsList); } else{ graphicalView = mPieGraph.getEmptyGraphicalView(getActivity()); } mPieChartContainer.removeAllViews(); mPieChartContainer.addView(graphicalView); } private int findValueInIntegerArrayList(int value, ArrayList<Integer> arrayList){ for (int i = 0; i < arrayList.size(); i++){ if (arrayList.get(i) == value){ return i; } } return -1; } private void setAccountSpinner(int indexOfCurrentItem, ArrayList<String> accountNames){ String[] arrayAccountNames = accountNames.toArray(new String[accountNames.size()]); ArrayAdapter<String> adapterAccount = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item_main_window, arrayAccountNames); adapterAccount.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mSpinnerAccount.setAdapter(adapterAccount); mSpinnerAccount.setSelection(indexOfCurrentItem); Log.d(LOG_CODE, "FragmentMainWindow ### SpinnerAccount set. Current Item: " + Integer.toString(indexOfCurrentItem) + ". Account names: " + accountNames); } } 

Fragment2:

 public class FragmentOperationWindow extends Fragment { public static final String NEW_OPERATION_ACCOUNT = "com.example.dtar2.moneymanager.ACCOUNT"; public static final String NEW_OPERATION_SPEND = "com.example.dtar2.moneymanager.SPEND"; public static final String NEW_OPERATION_CATEGORY = "com.example.dtar2.moneymanager.CATEGORY"; private static final String LOG_CODE = "LOG_CODE"; private final int REQUEST_NEW_CATEGORY = 1; private SpendingLab mSpendingLab; private String spendind = ""; private Spinner mSpinnerAccount; private Spinner mSpinnerCategory; private int currentAccountId = 0; private int currentCategoryId = 0; @Override public void onCreate (Bundle savedInstanceState){ super.onCreate(savedInstanceState); Log.d(LOG_CODE, "FragmentOperationWindow ### Opened operation fragment (onCreate now)."); mSpendingLab = SpendingLab.getInstance(getActivity()); } @Override public View onCreateView(final LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { final View v = inflater.inflate(R.layout.fragment_operation_window, parent, false); Log.d(LOG_CODE, "FragmentOperationWindow ### Operation fragment onCreateView"); //Making spinner for account choosing mSpinnerAccount = (Spinner) v.findViewById(R.id.spinner_account); setAccountSpinner(getArguments().getInt("account"), mSpendingLab.getAccountsNames()); mSpinnerAccount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (id == mSpendingLab.getAccountsNames().size() - 1){ Log.d(LOG_CODE, "FragmentOperationWindow ### User required creating new account dialog"); AlertDialog.Builder builderDialogNewAccount = new AlertDialog.Builder(getActivity()); final EditText editTextNewAccount = new EditText(getActivity()); builderDialogNewAccount.setTitle(getResources().getString(R.string.alert_dialog_new_account_title)); builderDialogNewAccount.setMessage(getResources().getString(R.string.alert_dialog_new_account_message)); builderDialogNewAccount.setView(editTextNewAccount); builderDialogNewAccount.setPositiveButton(getResources().getString(R.string.alert_dialog_button_create), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mSpendingLab.addAccountNameAtIndex(mSpendingLab.getAccountsNames().size() - 1, editTextNewAccount.getText().toString()); setAccountSpinner(mSpendingLab.getAccountsNames().size() - 2, mSpendingLab.getAccountsNames()); Log.d(LOG_CODE, "FragmentOperationWindow ### User added new account"); } }); builderDialogNewAccount.setNegativeButton(getResources().getString(R.string.alert_dialog_button_cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mSpinnerAccount.setSelection(currentAccountId); Log.d(LOG_CODE, "FragmentOperationWindow ### User pressed cancel in new account dialog"); } }); builderDialogNewAccount.setCancelable(true); builderDialogNewAccount.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { mSpinnerAccount.setSelection(currentAccountId); Log.d(LOG_CODE, "FragmentOperationWindow ### User canceled new account dialog by pressing back button"); } }); builderDialogNewAccount.show(); } } @Override public void onNothingSelected(AdapterView<?> parent) { //nothing to do } }); //Making spinner for category choosing mSpinnerCategory = (Spinner) v.findViewById(R.id.spinner_category); setCategorySpinner(currentCategoryId, mSpendingLab.getCategorysNames()); mSpinnerCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (id == mSpendingLab.getCategorysNames().size() - 1){ Log.d(LOG_CODE, "FragmentOperationWindow ### User required creating new category dialog"); FragmentManager fm = getActivity().getSupportFragmentManager(); FragmentCreateNewCategory fragment = new FragmentCreateNewCategory(); fragment.setTargetFragment(FragmentOperationWindow.this, REQUEST_NEW_CATEGORY); fm.beginTransaction().replace(R.id.fragment_main_window_container, fragment, "creating new category").addToBackStack(null).commit(); } } @Override public void onNothingSelected(AdapterView<?> parent) { //nothing to do } }); //Reading what user enterned in spending EditText editTextSpending = (EditText) v.findViewById(R.id.editText_spending); editTextSpending.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // } @Override public void afterTextChanged(Editable s) { spendind = s.toString(); } }); //Watching commit-button pressing Button buttonCommit = (Button) v.findViewById(R.id.button_commit); buttonCommit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendResult(Activity.RESULT_OK); } }); return v; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode, data); Log.d(LOG_CODE, "FragmentOperationWindow ### onActivityResult"); if (resultCode != Activity.RESULT_OK) return; if (requestCode == REQUEST_NEW_CATEGORY){ String newCategoryName = (String) data.getSerializableExtra(NEW_CATEGORY_NAME); int newCategoryColor = (int) data.getSerializableExtra(NEW_CATEGORY_COLOR); mSpendingLab.addCategoryNameAtIndex(mSpendingLab.getCategorysNames().size() - 1, newCategoryName); mSpendingLab.addColorId(newCategoryColor); currentCategoryId = mSpendingLab.getCategorysNames().size() - 2; setCategorySpinner(currentCategoryId, mSpendingLab.getCategorysNames()); } } private void sendResult(int resultCode){ if (getTargetFragment() == null){ return; } Intent i = new Intent(); i.putExtra(NEW_OPERATION_SPEND, spendind); i.putExtra(NEW_OPERATION_ACCOUNT, mSpinnerAccount.getSelectedItemPosition()); i.putExtra(NEW_OPERATION_CATEGORY, mSpinnerCategory.getSelectedItemPosition()); getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, i); getFragmentManager().popBackStack(); } private void setAccountSpinner(int indexOfCurrentItem, ArrayList<String> accountNames){ currentAccountId = indexOfCurrentItem; String[] arrayAccountNames = accountNames.toArray(new String[accountNames.size()]); ArrayAdapter<String> adapterAccount = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item_main_window, arrayAccountNames); adapterAccount.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mSpinnerAccount.setAdapter(adapterAccount); mSpinnerAccount.setSelection(indexOfCurrentItem); Log.d(LOG_CODE, "FragmentOperationWindow ### SpinnerAccount set. Current Item: " + Integer.toString(indexOfCurrentItem) + ". Account names: " + accountNames); } private void setCategorySpinner(int indexOfCurrentItem, ArrayList<String> accountNames){ String[] arrayCategoryNames = accountNames.toArray(new String[accountNames.size()]); ArrayAdapter<String> adapterCategory = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item_operation_window, arrayCategoryNames); adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mSpinnerCategory.setAdapter(adapterCategory); mSpinnerCategory.setSelection(indexOfCurrentItem); } } 

Logs (immediately after returning from the second fragment):

 FragmentMainWindow ### Returned from operation fragment. Account: 1. Category: 4. Spending: 6 FragmentMainWindow ### onCreateView FragmentMainWindow ### SpinnerAccount set. Current Item: 1. Account names: [Card, Cash, New account] FragmentMainWindow ### currentAccountId is - 1 FragmentMainWindow ### createNewPieGraph method was called FragmentMainWindow ### Current selected item in account spinner is - 1 FragmentMainWindow ### onItemSelected called (тот момент где onItemSelected не должен вызываться) FragmentMainWindow ### User changed account on Card. currentAccountId is - 1. Id in onItemSelected is - 0 FragmentMainWindow ### Current selected item in account spinner is - 0 

    1 answer 1

    I figured it out myself and found an interesting (in my opinion) Spinner property, which is not very obvious and can introduce newbies into a stupor. When calling setSelection, the onItemSelected method is called, but not from the place of the call, but only after the onCreateView method is completed. That is, if you setselection in onCreateView several times, only after it is completed will the onItemselected method be called as many times as setSelection /