1

i have two tabframents tab1 and tab2, on tab1 there is spinner and when item selected and swipe to tab2 and then back to tab1 spinner item selected is not retained. I found this which works for activity but not work in fragment.How can i retain the value of spinner item selected in tab1. What should add in my codes where and how to do it?

here is the code snipe

// custom spinner.xml

           <item
           android:id="@+id/spinner"
          app:actionViewClass="android.widget.Spinner"
            app:showAsAction="always" />  

// menu

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.spinner, menu);
    super.onCreateOptionsMenu(menu, inflater);
    final MenuItem item = menu.findItem(R.id.spinner);
    spinner = (Spinner) MenuItemCompat.getActionView(item);
    ArrayList<String>  itemList= new ArrayList<String>();
    itemList.add("itemA");
    itemList.add("itemB");
    spinner.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, itemList));
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
            String item2 = parent.getItemAtPosition(position).toString();

                Toast.makeText(getContext(), item2, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}
Community
  • 1
  • 1
f2k
  • 99
  • 11
  • i saw your solution https://stackoverflow.com/questions/20209015/how-do-i-preserve-the-state-of-a-selected-spinner-dropdown-item-on-orientation-c can you helpme with the above question? @umair.ali – f2k May 23 '17 at 09:59
  • @EagleEye can you help with above question i have seen this from you https://stackoverflow.com/questions/28184728/how-to-retain-the-selected-item-in-spinner – f2k May 23 '17 at 10:31

0 Answers0