Lakshman Pilaka
Lakshman Pilaka

Reputation: 1951

Going to previous activity with Intent Extras

I have two activities. One has a a form with few EditTexts. The values in these inputs should come from a master list and so, on focus of the EditText, I am taking the control to the another activity which uses 'android.support.v7.widget.SearchViewwith aListView`

OnFocus of a EditText I invoke the SearchActivity with following code:

    ETVehicleRegnLoc.setOnFocusChangeListener(new View.OnFocusChangeListener()  {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                startActivity(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"));
            }
        }
    });

Once the value is selected I am sending the value back with the following code

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {

                Intent intent = new Intent(SearchActivity.this , VehicleActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("SelectedVal", dataArray.get(arg2).toString());
                intent.putExtras(bundle);
                startActivity(intent);

//                Toast.makeText(SearchActivity.this, dataArray.get(arg2).toString(), Toast.LENGTH_SHORT).show();
            }
        });

This works fine. But because I am calling the VehicleActivity through Intent, its clearing the entire form of the values which are already entered.

But when I press the "back" button on the mobile, the values persist. But the purpose of going to the SearchActivity is not met :)

I think I have to simulate the back button press. how can i do it??

Upvotes: 0

Views: 523

Answers (1)

Geethakrishna Juluri
Geethakrishna Juluri

Reputation: 540

Use

startActivityForResult(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"),2);

instead of

startActivity(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"));

And in onItemClickListener Do

Intent intent=new Intent();  
                intent.putExtra("MESSAGE",message);  
                setResult(2,intent);  
                finish();

And get the data in OnActivityForResult of your VehicleActivity

Upvotes: 1

Related Questions