我有一个实现接口(interface) (InterfaceA) 的 Activity (ActivityA)。 当我想显示其他 Activity 时,我使用此代码:
Intent intent = new Intent(this, ActivityB.class);
// intent.putExtra("interface", this);
startActivity(intent);
和 Activity A
public class ActivityA extends Activity implements InterfaceA, Serializable
然后在ActivityB中
Intent intent = getIntent();
Serializable s = intent.getExtras().getSerializable("interface");
if(s != null && s instanceof InterfaceA)
iInterface = (InterfaceA)s;
但是我在执行过程中遇到错误
android java.io.NotSerializableException
是否可以通过 Intent 发送 Activity (如 Objective-C 中的委托(delegate)?
谢谢
最佳答案
In ActivityB, I'm creating a new local object I want to display in ActivityA without having to refresh the listView in ActivityA.
您可以为 ActivityA
中的结果启动 ActivityB
,因为这不会重新创建 ActivityA
。当您从 ActivityB
收到结果时,在列表中添加新对象并调用适配器的 notifyDataSetChanged()
。
在 ActivityA 中,
public static final int REQ_CODE = 1;
// Starting an activity
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQ_CODE );
并重写这个方法得到结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_CODE)
{
if(resultCode == Activity.RESULT_OK)
{
// Add the object in list and call adapter's notifyDataSetChanged()
}
}
}
在ActivityB中,
Intent data = new Intent();
// Put the new object as Serializable in Intent
setResult(Activity.RESULT_OK, data);
// And finally, close the activity
finish();
关于Android通过Intent发送接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35623418/