Snowrain
Snowrain

Reputation: 3

Need help on Intent Android

How are you all? New on Intent and do not know much about it. I have two activities. First activity has a button to start second activity. Here is how:

 private void main_button_start_from_long_clicklistener()

{

  main_button_start_from.setOnLongClickListener(new OnLongClickListener()
  {

    @Override
    public boolean onLongClick(View vlc)
    {
        // TODO Auto-generated method stub
        intent_main = new Intent(getApplicationContext(),startfrom.class);
        startActivity(intent_main);
        return false;
    }

  });

}

Now second activity has an Edittext to take input and send to first activity to display. Here is the code:

 edittext_input.setInputType(InputType.TYPE_CLASS_NUMBER);

 edittext_input.setOnEditorActionListener(new EditText.OnEditorActionListener()
     {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) 
        {
            if(actionId==EditorInfo.IME_ACTION_DONE);
            {
                string_start_from_input = edittext_input.getText().toString();
                   intent_start_from_input = new Intent();
                   intent_start_from_input.putExtra("input-value", string_input_start_from);
                   setResult(RESULT_OK, intent_start_from_input);
                   finish();
            return false;
            }

        }

     });

And first activity has also:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
      if(requestCode == REQUEST_CODE_FUNCTION)
          if(requestCode == RESULT_OK)
          {
              string_input = data.getStringExtra("input_value");
          }
        super.onActivityResult(requestCode, resultCode, data);
    }

Now how to display the String on first activity in

public class MainActivity extends Activity 
{
.....
 startActivityForResult(?, REQUEST_CODE_FUNCTION);

Thankyou.

Upvotes: 0

Views: 41

Answers (1)

Ralph Bergmann
Ralph Bergmann

Reputation: 3076

In your main_button_start_from_long_clicklistener change the startActivity(intent_main); to startActivityForResult(intent_main, 0);

And than override the onActivityResult in your first activity. see Starting Activities and Getting Results

Upvotes: 1

Related Questions