0

I am building an android application Where on click of an Image-view the OnBackPressed Function is called.

Here is code -

    ImageView imgv = (ImageView) mCustomView.findViewById(R.id.back);

    imgv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub

            onBackPressed();
        }
    });

Now The Problem is how To send an Text-View String With this OnBackPressed function.

Test123
  • 368
  • 1
  • 7
  • 26

1 Answers1

3

When you want to start second activity use this code :

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

When you want to come back to first activity use this code :

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();

And finally in the first activity you can get the returned value by the following method :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if(resultCode == RESULT_OK) {
            String result = data.getStringExtra("result");
        }
        if (resultCode == RESULT_CANCELED) {
            // If there is no result 
        }
    }
}

AN EXAMPLE :

So for using the above code for developing the example that you said :

in forst activity start second activity :

public void ButtonClickedMethod(View v)
{
    Intent i = new Intent(this, Second.class);
    startActivityForResult(i, 1);
}

then in the on back pressed method of second activity use this code :

@Override
public void onBackPressed()
{
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", ((TextView) findViewById(R.id.text)).getText());
    setResult(RESULT_OK,returnIntent);
    finish();
}

Again on the first activity use this code :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == 1) {
        if(resultCode == RESULT_OK) {
            String result = data.getStringExtra("result");
            ((Button) findViewById(R.id.button1)).setText(result);
        }
        if (resultCode == RESULT_CANCELED) {
            // If there is no result 
        }
    }
}
Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
  • Sorry Might you understand wrong. Let me More clear. In First Activity There is an Button my click on it I move to Second Activity now In Second Activity There is an TextView and an ImageView on Click of ImageView I again go to Firstactivity. now In first activity button text should be change find the parse value of text – Test123 Nov 16 '14 at 14:34
  • @Sonam The code is OK ,just you should use it for doing your development , I edited the answer and added the example of using the code , This is the code that you need in your application , This code will open second activity and when you press back button on the first activity button text will change to textView that exists on second activity , Feel free to ask your questions ;) – Morteza Soleimani Nov 16 '14 at 14:50
  • Thanks I am doing this in onBackPressed Funtion for checking really my value is parsing or not by toasting the value. But the toast value is null. here is my code - String arg = null; Intent returnIntent = new Intent(); returnIntent.putExtra(arg, txtmapinfo.getText()); setResult(RESULT_OK,returnIntent); finish(); Toast.makeText(getApplicationContext(), arg, Toast.LENGTH_LONG).show(); – Test123 Nov 16 '14 at 15:01
  • Toast.makeText(getApplicationContext(), arg, Toast.LENGTH_LONG).show(); should be before the finish() method not after it. – Morteza Soleimani Nov 16 '14 at 15:03