//第一个activity
package bwei.com.startactivityforresult; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void tiao(View view) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==1&&resultCode==1){ String text = data.getStringExtra("text"); Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show(); } } }
//第二个activity
package bwei.com.startactivityforresult; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); TextView edit = findViewById(R.id.edit); Intent intent = getIntent(); intent.putExtra("text", edit.getText().toString()); setResult(1, intent); finish(); } }