I am working on an app and I need to pass the contents of some textviews to a new activity, but I want to save the content I pass while the app is open so that the user can select more items from other activities and send them to the final checkout activity.

right now I have spinners which save the selection to a Textview

public class Americano extends AppCompatActivity  {

// MyDBHandler dbHandler;

String result;
TextView tvSize;
Spinner spinner;
int mPos;
String mSelection;


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

    Spinner spinnerSize = (Spinner) findViewById(R.id.spinner_size);
    AdapterView.OnItemSelectedListener listener = new myOnItemSelectedListener();
    spinnerSize.setOnItemSelectedListener(listener);

    Spinner spinnerSyrups = (Spinner) findViewById(R.id.spinner_syrups);
    AdapterView.OnItemSelectedListener listenerSyrups = new myOnItemSelectedListener2();
    spinnerSyrups.setOnItemSelectedListener(listenerSyrups);

    Spinner spinnerTopping = (Spinner) findViewById(R.id.spinner_toppings);
    AdapterView.OnItemSelectedListener listenerTopping = new myOnItemSelectedListener3();
    spinnerTopping.setOnItemSelectedListener(listenerTopping);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DBAdapter dbAdapter = new DBAdapter(view.getContext());


        }
    });

}

public class myOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
        Americano.this.mPos = pos;
        Americano.this.mSelection = parent.getItemAtPosition(pos).toString();
        TextView resultText = (TextView) findViewById(R.id.tvSize);
        resultText.setText(Americano.this.mSelection);
    }

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

    }
}

public class myOnItemSelectedListener2 implements AdapterView.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
        Americano.this.mPos = pos;
        Americano.this.mSelection = parent.getItemAtPosition(pos).toString();
        TextView resultText = (TextView) findViewById(R.id.tvSyrup);
        resultText.setText(Americano.this.mSelection);
    }

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

    }
}

public class myOnItemSelectedListener3 implements AdapterView.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
        Americano.this.mPos = pos;
        Americano.this.mSelection = parent.getItemAtPosition(pos).toString();
        TextView resultText = (TextView) findViewById(R.id.tvTopping);
        resultText.setText(Americano.this.mSelection);
    }

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

    }
}

I have more menu item classes exactly like this one

I want to pass the textview data to the checkout page. But save that data in that page until the user closes the app.

thanks

1 Answers

1
ARP On Best Solutions

You can do it sending the information through an Intent. In your listener you would go to another actvity like this:

Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("RESULT_TEXT", resultText);
startActivity(intent);

And on the NewActivity you can get the information like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Bundle bundle = getIntent().getExtras();
    String resultText = bundle.getString("RESULT_TEXT", "");
}

Hope it helps.