2

I'm developing a cashier/order-taking application. I use a spinner in order to note the quantity of an item. However, after a user selects an option on a spinner, the UI is reset back to its original onCreate stage.

Kind of like when the UI resets after a screen rotation, I avoided this specific issue by using android:configChanges="keyboardHidden|orientation|screenSize"> in the manifest, but I can't figure out how to do the same after a spinner selection.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Spinner Declaration:
        Spinner quantitySpinner = findViewById(R.id.quantityDropDown);

        //Spinner Adapter:
        ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.quantity_array, android.R.layout.simple_spinner_item);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //Connect Spinner to Adapter:
        quantitySpinner.setAdapter(spinnerAdapter);


        quantitySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                int index = arg0.getSelectedItemPosition();
                index = index + 1;
                String text =  "" + index;
                System.out.println(text);
            }

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

        });
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
StyleOver
  • 73
  • 9
  • make use of savedInstanceState, there is a callback which you need to implement "onSaveInstanceState". it would be best if you can make use of ViewModel from the android architecture components – Pankaj Nimgade Sep 29 '19 at 02:28
  • You are looking for : https://stackoverflow.com/a/20209361/1318946 – Pratik Butani Oct 03 '19 at 07:39
  • I fixed the problem, basically, I was creating everything in one class, and ended up just making multiple classes for each page/scene using the new intent activity to switch scenes, etc. This not only made things more organized but made it look more professional animation wise. – StyleOver Oct 07 '19 at 19:01
  • 1
    And then I used savedInstanceState, so Pankaj if you would like to make that an answer I could mark it as the solution @PankajNimgade – StyleOver Oct 17 '19 at 06:24
  • 1
    @StyleOver, Its okay mate, I am glad it worked for you :) – Pankaj Nimgade Oct 25 '19 at 05:11

0 Answers0