-1

I'm using two activities, MainActivity & SubActivity.

MainActivity.class

public class MainActivity extends AppCompatActivity {
    Button button;

    @Override
    protected void onCreate() {
        ...
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent sub = new Intent(this, SubActivity.class);
                sub.putExtra("name", "a");
                startActivity(sub);
            }
        }
    }
}

SubActivity

public class SubActivity extends AppCompatActivity {
    EditText text;
    @Override
    protected void onCreate() {
        ...
        text.setText(getIntent().getStringExtra("name"));
    }

    @Override
    public void onBackPressed() {
       super.onBackPressed();
    }
}

In this case, if I change the text in EditText in SubActivity, I want to send changed text data to MainActivity when back button pressed in SubActivity(It means SubActivity onDestroy()). I don't want to make the second MainActivity like this in SubActivity.

@Override
public void onBackPressed() {
    Intent intent = new Intent (this, MainActivity.class);
    intent.putExtra("name", text.getText().toString());
    startActivity(intent);
}

Is there any way to send text data when SubActivity removed from the activity stack?

hanjiman
  • 465
  • 1
  • 6
  • 17

4 Answers4

2
  public class MainActivity extends AppCompatActivity {
      Button button;

     @Override
     protected void onCreate() {
      ...
       button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
               Intent sub = new Intent(this, SubActivity.class);
              sub.putExtra("name", "a");
              startActivityForResult(sub , 2);
        }
    }
}
@Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data)  
   {  
             super.onActivityResult(requestCode, resultCode, data);  
              // check if the request code is same as what is passed  here it is 2  
               if(requestCode==2)  
                     {  
                        String message=data.getStringExtra("name");   
                        textView1.setText(message);  
                     }  
   }  
}

And

      @Override
       public void onBackPressed() {
        Intent intent = new Intent (this, MainActivity.class);
        intent.putExtra("name", text.getText().toString());
        setResult(2,intent);  
    }
ashok
  • 431
  • 5
  • 8
0

You need to use startActivityForResult() if you expect to have a return from the started activity.

After that, you'll also need to implement onActivityResult() method on the start activity to actually read the returned data.

ariefbayu
  • 21,849
  • 12
  • 71
  • 92
0

Use startActivityForResult instead of startActivity

MainActivity :

    public class MainActivity extends AppCompatActivity {
    Button button;
    String name= "a";
    @Override
    protected void onCreate() {
        ...
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent sub = new Intent(this, SubActivity.class);
                sub.putExtra("name", name);
                startActivityForResult(sub,100);
            }
        }
    }

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == 100) {
              String name = data.getStringExtra("name);
                text.setText(name);
            } 
         }
      }
}

SubActivity:

public class SubActivity extends AppCompatActivity {
    EditText text;
    @Override
    protected void onCreate() {
        ...
        text.setText(getIntent().getStringExtra("name"));
    }

    @Override
    public void onBackPressed() {
       Intent resultIntent = new Intent();
       resultIntent.putExtra("name", "your_editext_value");
       setResult(Activity.RESULT_OK, resultIntent);
       finish();
    }
}
Rajasekaran M
  • 2,478
  • 2
  • 20
  • 30
0

You can use SharedPreference for storing value and update it or use it anywhere instead of using Intent

like In MainActivity

public class MainActivity extends AppCompatActivity {
Button button;

@Override
protected void onCreate() {
    ...

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
  Editor edit=sp.edit();
  edit.putString("name","a");
  edit.commit();
            Intent sub = new Intent(this, SubActivity.class);

            startActivity(sub);
        }
    }
} }

and In SubActivity

public class SubActivity extends AppCompatActivity {
EditText text;
@Override
protected void onCreate() {
    ...
    text.setText(sp.getString("name",""));
text.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
  Editor edit=sp.edit();
 edit.putString("name",s.toString);
  edit.commit();
} });
}

@Override
public void onBackPressed() {

   super.onBackPressed();
}}

To use value from SharedPreference

sp.getString("name","");

For using SharedPreference look at this https://developer.android.com/training/data-storage/shared-preferences#java

S.Ambika
  • 292
  • 1
  • 14