0

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 Answer 1

1

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.

8
  • If I left the NewActivity and went to a ThirdActivity for example would the info still be there when I returned to the NewActivity? Mar 18, 2016 at 19:15
  • 1
    yes as long as the activity has not called .finish()
    – Petro
    Mar 18, 2016 at 19:18
  • If you don't destroy NewActivity (or is destroyed by the system) the information will still be there.
    – ARP
    Mar 18, 2016 at 19:18
  • one last question, i'm using the floating action button to send the data, it says it cant resolve 'resultText'? Mar 18, 2016 at 19:23
  • That is because you have to make resultText variable to global: String resultText = ""; @Override protected void onCreate(Bundle savedInstanceState) { Bundle bundle = getIntent().getExtras(); resultText = bundle.getString("RESULT_TEXT", ""); }
    – ARP
    Mar 18, 2016 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.