完全可以,只需要往intent里面添加一个标识数据即可
//两个activity
//第一个activity
public class OneActivity extends Activity{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(OneActivity.this,EditActivity.class);
intent.putExtra("from",1);
startActivity(intent);
}
});
}
}
//第二个activity
public class TwoActivity extends Activity{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(OneActivity.this,EditActivity.class);
intent.putExtra("from",2);
startActivity(intent);
}
});
}
}
编辑activity,判断intent传进来的from数据,为1即为OneActivity,2为TwoActivity
public class EditActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
int from = intent.getIntExtra("from", 0);
switch(from){
case 1:
//OneActivity
break;
case 2:
//TwoActivity
break;
//case .......
}
}
}
如果你需要判断Intent传得数据来源的话,可以在传数据的同时,再传一个当前activity的名字,再另一个activity接收时可以根据此名字去判断即可,比如:
在Activity A:
Intent intent=new Intent(A.this,B.class);
intent.putExtra("activity","当前activity名字");
intent.putExtra("data","你的数据");
startActivity(intent);
在Activity B:
Intent intent=getIntent();
if(intent!=null){
if(intent.getExtra()!=null){
String activity=intent.getStringExtra("activity");
if(activity=="B"){
//doSomething
}
String data=intent.getStringExtra("data");
//doSomething
}
}
多个Activity向一个Activity传递数据还真没听说过,不知道楼主是怎么实现的,但是如果一定要区别的话就加一个键值对作为标记来区分了,intent传递数据不就是用的键值对吗,你加一个标记就可以了