putExtra in Spinner

0

Hello, in my project I have a Spinner of id @+id/spnList , which is in my activity Main . This Spinner is populated with tables created dynamically in the database, so everything works perfectly.

The user clicks on a button where he inserts items. When I click the button to go to the InsertProduct activity, I give putExtra to get the chosen table in the Spinner of activity Main so that the item is added to the table chosen by the user. After it adds the item, it returns to the Main activity, but when it returns to activity Main the selected list in Spinner is the first. Let's suppose I have five lists and the user selects and adds an item to Lista 5 , when returning to the Main activity the selected list in Spinner is Lista 1 and not Lista 5 as the user selected previously. I tried to give putExtra of activity InsertProduct to Main , but it returns as null
Ex:

-- Main --
// Função do botão de adicionar item
    Spinner spn = (Spinner) findViewById(R.id.spnList);
    String strSpn = spn.getSelectedItem().toString(); // Vamos dizer que o valor sera "Lista 5"
    Intent i = new Intent(this, InsertProduct.class);
    i.putExtra("selectedItem", strSpn);
    startActivityForResult(i, 1);

// Função que exibi os itens na tela
    Controller controller = new Controller(getBaseContext());

    String[] list = controller.getData();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spnList.setAdapter(adapter);
    if (getIntent() != null && getIntent().getExtras() != null) {
        Bundle bundle = getIntent().getExtras();
        if (!bundle.getString("setSelectedItem").equals(null)) {
            String setSpnSelected = bundle.getString("setSelectedItem");
            int spinnerPosition = adapter.getPosition(setSelectedItem);
            spnList.setSelection(spinnerPosition);
        }
    }

-- InsertProduct --
    Bundle extras = getIntent().getExtras();
    String strSpn = extras.getString("selectedItem");

    String result = ... // Adiciona item a tabela "Lista"

    Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", result);
    returnIntent.putExtra("setSelectedItem", strSpn);
    setResult(Activity.RESULT_OK, returnIntent);
    finish();

Does anyone know how I can keep the table selected by the user even after I go to another activity?

    
asked by anonymous 21.04.2017 / 23:35

1 answer

1

You can save the Activit state by overwriting the onSaveInstanceState () method and restoring with the onRestoreInstanceState () method.

It would be more or less like this.

void onSaveInstanceState (Bundle outState){
    outState.putCharSequence("selectedItem", spn.getSelectedItem());
}

void onRestoreInstanceState (Bundle savedInstanceState){
    spn.setSelectedItem(outState.getCharSequence("selectedItem"));
}
    
22.04.2017 / 01:47