需求分析
从MainActivity-->BActivity-->CActivity-->MainActivity
也就是说跳到第三个界面后跳回到第一个界面,并把值从第三个界面传到第一个界面,没错就是这个骚操作~
栗子.gif栗子惯例,先上GIF
MainActivity.java
public class MainActivity extends Activity {
private Button btnOne;
private TextView tvContentOne;
private final int FIRST_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOne = (Button) this.findViewById(R.id.btnOne);
tvContentOne = (TextView) this.findViewById(R.id.tvContentOne);
btnOne.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//跳转到第二个界面,并把值传过去
Intent intent = new Intent(MainActivity.this,BActivity.class);
intent.putExtra("One", "我是从第一个传过来的内容");
startActivityForResult(intent, FIRST_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//接受从第三个界面的返回值
if(requestCode==FIRST_REQUEST_CODE && resultCode==Activity.RESULT_FIRST_USER){
if (data != null) {
tvContentOne.setText(data.getStringExtra("request"));
}
}
//接受从第二个界面的返回值
else if(requestCode==FIRST_REQUEST_CODE && resultCode==Activity.RESULT_OK){
if (data != null) {
tvContentOne.setText(data.getStringExtra("request"));
}
}
}
}
BActivity.java
public class BActivity extends Activity{
Button btnTwo;
Button btnTwo2;
TextView tvContentTwo;
private final int SECOND_REQUEST_CODE = 2;
private String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
btnTwo = (Button) findViewById(R.id.btnTwo);
btnTwo2 = (Button) findViewById(R.id.btnTwo2);
tvContentTwo = (TextView) findViewById(R.id.tvContentTwo);
text = getIntent().getStringExtra("One");
if(text != null){
//设置第一个界面传过来的内容
tvContentTwo.setText(text);
}
btnTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//返回第一个界面,把值传过去
Intent intent = new Intent();
intent.putExtra("request", "我是从第二个界面返回的内容");
setResult(Activity.RESULT_OK,intent);
finish();
}
});
btnTwo2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//跳转到第三个界面,把第二个界面的内容传过去
Intent intent = new Intent(BActivity.this,CActivity.class);
intent.putExtra("Two", "我是从第二个界面传过来的内容");
startActivityForResult(intent, SECOND_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//关键的地方,接收到第三个界面传过来的值,并把值传回第一个界面,并关闭第二个界面
if(requestCode==SECOND_REQUEST_CODE && data != null){
if (data != null) {
setResult(Activity.RESULT_FIRST_USER, data);
finish();
}
}
}
}
CActivity.java
public class CActivity extends Activity{
Button btnThree;
TextView tvContentThree;
private String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
btnThree = (Button) findViewById(R.id.btnThree);
tvContentThree = (TextView) findViewById(R.id.tvContentThree);
text = getIntent().getStringExtra("Two");
if(text != null){
//设置第二个界面传过来的值
tvContentThree.setText(text);
}
btnThree.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//把值传回第二个界面,由第二个界面传回第一个界面
Intent intent = new Intent();
intent.putExtra("request", "我是从第三个界面返回的内容");
setResult(Activity.RESULT_FIRST_USER,intent);
finish();
}
});
}
}
总结:开发中的小技巧,希望对大家有帮助~~
未经本人允许禁止转载,违者必究
欢迎加入QQ交流群657206000点我加入 |
---|
QQ交流群:657206000 |
网友评论