2

Once the user chooses a product from my ListView, it then puts the selected text from that ListView into an EditText. The problem I am having is when the user selects a product from the list, and then presses back, it comes up with the list again instead of returning to the EditText activity.

I have tried using "finish();" after the activity starts but nothing seems to be working.

Activity that holds the EditText that launches the List activity:

        EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
    CPU.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent CPUList = new Intent(getApplicationContext(),
                    CPUList.class);
            startActivityForResult(CPUList, 1);
            Intent i = getIntent();
            String product = i.getStringExtra("key");
            EditText CPU = ((EditText) findViewById(R.id.autoCompleteTextView4));
            CPU.setText(product);

        }
    });

List view class

@Override
public void onCreate(Bundle OnsaveInstanceState) {
    super.onCreate(OnsaveInstanceState);
    setContentView(R.layout.activity_cpulist);

    ListView listViewCPU = (ListView) findViewById(R.id.listViewCPU);
    listViewCPU.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    String CPUList[] = {

 "CPU's go here", "CPU's go here", "CPU's go here", "CPU's go here" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, CPUList);

    listViewCPU.setAdapter(adapter);

    listViewCPU.setOnItemClickListener(new OnItemClickListener() {

 @Override
 public void onItemClick(AdapterView<?> listview, View myView,
        int pos, long mylng) {
    String CPU = (String) listview.getAdapter().getItem(pos);

    Intent i = new Intent();
    i.putExtra("key", CPU);
    setResult(1, i);

    finish();

    } 
});

2 Answers 2

2

You need to launch your activity in a way that it doesn't get added to back stack. Here's how you do that: https://stackoverflow.com/a/12358563/375929

0

If I understand you correctly, you are calling finish() on the wrong Activity. If you want the list Activity to finish then that's where you need to call finish()

  @Override
    public void onItemClick(AdapterView<?> listview, View myView,
            int pos, long mylng) {
        String CPU = (String) listview.getAdapter().getItem(pos);

        Intent i = new Intent(getApplicationContext(),
                ListmenuActivity.class);

        i.putExtra("key", CPU);
        startActivity(getIntent());
        startActivity(i);

        finish();  // finish here

    }

and remove finish() from your EditText Activity

Another issue I see is it looks like you are starting that second bit of code with the first using startActivityForResult() but you aren't sending back a result in your second code. Instead, you seem to be starting another Activity. It seems that second bit should be more like

 @Override
    public void onItemClick(AdapterView<?> listview, View myView,
            int pos, long mylng) {
        String CPU = (String) listview.getAdapter().getItem(pos);

        Intent i = new Intent();
        i.putExtra("key", CPU);
        setResult(1, i);

        finish();  // finish here

    }
6
  • I'm confused on your flow. What is ListmenuActivity and which do you want to close?
    – codeMagic
    Commented Aug 8, 2013 at 16:50
  • ListmenuActivity is the activity that holds the EditText, and I want to close CPUList once the user has chosen their product.
    – Jack
    Commented Aug 8, 2013 at 16:57
  • That's now fixed that issue I was having but now once I click a product from the ListView, it doesn't appear in the EditText for some odd reason, thanks anyway.
    – Jack
    Commented Aug 8, 2013 at 17:03
  • Please accept if it fixed your issue. The second issue is probably because all of the code you have after finish() in the onClick() should be inside onActivityResult(). See the Docs for how to use that appropriately
    – codeMagic
    Commented Aug 8, 2013 at 17:07
  • Updated my code and have put "startActivityForResult(CPUList, 1);" before the code(see above). Nothing seems to be working, will you be able to assist me, I've looked at the "See the docs", I can't find the issue here, thanks.
    – Jack
    Commented Aug 8, 2013 at 17:39

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