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