0

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??

Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48
  • 1
    http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android – sasikumar Mar 10 '16 at 10:59
  • In focus change call startActivityForResult. Then on back press from Search screen, set the bundle parameters in intent extras and call finish. – Febi M Felix Mar 10 '16 at 11:00

1 Answers1

1

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