Android 将接口从类传递到活动
如果我的类中有一个对象表单接口,如何将其传递给activity 我的解决方案是将其更改为静态对象,它工作正常,但有时会因为垃圾收集器无法收集的所有旧引用而导致内存泄漏,并且我无法在接口上实现“可序列化”Android 将接口从类传递到活动,android,android-intent,interface,serializable,Android,Android Intent,Interface,Serializable,如果我的类中有一个对象表单接口,如何将其传递给activity 我的解决方案是将其更改为静态对象,它工作正常,但有时会因为垃圾收集器无法收集的所有旧引用而导致内存泄漏,并且我无法在接口上实现“可序列化” public class MyClass { protected OnStringSetListener onstringPicked; public interface OnStringSetListener { void OnStringSe
public class MyClass {
protected OnStringSetListener onstringPicked;
public interface OnStringSetListener {
void OnStringSet(String path);
}
//......//
public void startActivity(){
Intent intent=new Intent(context,Secound.class);
// ?! intent.putExtra("TAG", inter);
context.startActivity(intent);
}
}
传入自定义对象稍微复杂一些。您可以将该类标记为可序列化 让Java来处理这个问题。然而,在android上,使用Serializable会严重影响性能。解决方法是使用可包裹的
传入自定义对象稍微复杂一些。您可以将该类标记为可序列化 让Java来处理这个问题。然而,在android上,使用Serializable会严重影响性能。解决方法是使用可包裹的 在我看来,使用将是一个不错的选择。实际上捆绑包内部使用。因此,这种方法将是一种很好的方法 假设您想将class
MyClass
类型的对象传递给另一个活动,您所需要的只是添加两种方法来压缩/解压bundle
要捆绑:
public Bundle toBundle(){
Bundle bundle = new Bundle();
// Add the class properties to the bundle
return bundle;
}
从捆绑包:
public static MyClass fromBundle(Bundle bundle){
MyClass obj = new MyClass();
// populate properties here
return obj;
}
注意:然后您可以使用putExtra
将捆绑包传递给其他活动。请注意,处理包裹比处理包裹简单得多。在我看来,使用将是一个不错的选择。实际上捆绑包内部使用。因此,这种方法将是一种很好的方法
假设您想将classMyClass
类型的对象传递给另一个活动,您所需要的只是添加两种方法来压缩/解压bundle
要捆绑:
public Bundle toBundle(){
Bundle bundle = new Bundle();
// Add the class properties to the bundle
return bundle;
}
从捆绑包:
public static MyClass fromBundle(Bundle bundle){
MyClass obj = new MyClass();
// populate properties here
return obj;
}
注意:然后您可以使用
putExtra
将捆绑包传递给其他活动。请注意,处理包裹比包裹简单得多。使用Parcelable
。。它是为Android构建的。:)使用Parcelable
。。它是为Android构建的:)