Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid spinner not retaining value when activity is changed (lifecycle related)
    primarykey
    data
    text
    <p>In my activity, I have a Spinner and a ListView. The Spinner is used to filter the items in the ListView. When the user clicks on a list item, another activity is called, which shows the details of the list item. Then, from the details activity, the user presses the back button, and the spinner (thus the the list) gets reset to its initial value.</p> <p>This is due to the fact that I populate the spinner's adapter, and declare its onItemSelectedListener, into the onStart() method, which is called again when the user presses the back button. Also the onItemSelectedListener is actually called once by the onStart() method.</p> <p>How do I avoid this behavior? I guess I should move the listener and the adapter somewhere else, but where?</p> <p>Here is an overview of the class</p> <pre><code>public static ArrayList&lt;String&gt; years; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.archive); // populate years array here } @Override protected void onStart() { super.onStart(); Spinner spinner = (Spinner) findViewById(R.id.spinner_anni); // create spinner adapter ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, ArchiveActivity.years); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); // create spinner listener spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView&lt;?&gt; parentView, View selectedItemView, int position, long id) { filterList(ArchiveActivity.years.get(position)); } @Override public void onNothingSelected(AdapterView&lt;?&gt; parentView) { return; } }); // create list listener ListView list = (ListView) findViewById(R.id.list_archive); list.setOnItemClickListener(this); } private void filterList(String year) { // filter list data here } @Override public void onItemClick(AdapterView&lt;?&gt; list, View item, int pos, long id) { // call detail activity for list item that's been pressed Intent i = new Intent(this, DetailActivity.class); startActivity(i); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload