Question

How to get EditText's value from sub-Activity? With a condition that if I click back button on the phone, there's no error in Sub-Activity?

This is my Sub-Activity code:

    public class SBooksSearch extends Activity {
    private EditText mTextSearch;   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.sbooks_search); 

        mTextSearch = (EditText)findViewById(R.id.edit_search);     
        Button searchButton = (Button)findViewById(R.id.btn_search);        

        searchButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){                
                Intent data = new Intent();             
                data.putExtra(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());         
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }   

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);        
    }
    @Override
    protected void onPause(){
        super.onPause();

    }
    @Override
    protected void onResume(){
        super.onResume();       
    }
}

This is my Activity-Result:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);        
switch(requestCode){
case ACTIVITY_SEARCH:
Bundle extras = getIntent().getExtras();
mTitleRaw = extras != null ? extras.getString(SBooksDbAdapter.KEY_TITLE_RAW) : null;            
     if(mTitleRaw!=null){
      Cursor cursor = mDbHelper.searchData(mTitleRaw);

    String[] from = new String[]{ SBooksDbAdapter.KEY_ROWID,
                        SBooksDbAdapter.KEY_TITLE, SBooksDbAdapter.KEY_LYRICS };
        int[] to = new int[]{ R.id.id, R.id.title, R.id.lyrics };
        SimpleCursorAdapter adapter = 
                    new SimpleCursorAdapter(this, R.layout.sbooks_row, cursor, from, to );
           setListAdapter(adapter);
            }           
           break;
        }
    }
Was it helpful?

Solution

First of all, you shouldn't be attempting any sort of action if the user hits the "back" button. It's a global button that means "get me out of here now", and it's usually understood that the user wants nothing other than going back one screen to occur as a result of that.

So what you need to do is in your searchButton.setOnClickListener, in the onClick, create an empty Intent like so:

Intent data = new Intent();

Then you need to be adding the value of your EditText as an extra value, like so:

data.putExtra(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());

Finally, include this intent in your setResult call:

setResult(RESULT_OK, data);

In your onActivityResult pull the value out of the intent like you're already doing and you should be fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top