Android:如何将接口从一个活动发送到另一个活动 [英] Android: How to send interface from one activity to another

查看:109
本文介绍了Android:如何将接口从一个活动发送到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的界面:

public interface MyInterface {
    public void aMethod();
}

我的自定义对象:

public class MyObject {

    private Context context;
    private MyInterface inter;

    public MyObject(Context context) {
        this.context = context;
        this.inter = (MyInterface) this.context;
        inter.aMethod();
    }
}

MainActivity:

MainActivity:

public class MainActivity extends Activity implements MyInterface {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyObject my = new MyObject(this);

    }

    @Override
    public void aMethod() {
        Toast.makeText(this, "done", Toast.LENGTH_SHORT).show();
    }
}  

这里,在MyObject构造函数中我可以从中获取接口上下文然后与Activity通信。

Here, inside MyObject constructor i can get the interface from the context and then communicate with the Activity.

但是如何将界面从一个活动发送到另一个?

我需要在Activity2中调用Activity1中的方法

有没有这样的方式?

注意:我不想使用片段。

推荐答案

首先,这是非常糟糕的:

First and foremost, this is very bad:

this.inter = (MyInterface) this.context;

如果将上下文传递给未实现接口的构造函数,应用程序将崩溃并且很容易犯这样的错误。所以你看,这很容易出错,而是像这样实现它:

If you pass a context into the constructor which does not implement your interface your application will crash and it's easy to make such a mistake. So you see, this is very error prone, instead implement it like this:

public class MyObject {

    private Context context;
    private MyInterface inter;

    public MyObject(Context context, MyInterface inter) {
        this.context = context;
        this.inter =  inter;
        inter.aMethod();
    }
}

这种方式更安全,更清洁。

This way it's much safer, and cleaner.

对象发送到另一个活动确保它实现 Serializable ,如下所示:

To send the Object to another Activity make sure it implements Serializable like this:

public interface MyInterface extends Serializable {
    public void aMethod();
}

现在你可以将界面作为额外添加到 Intent 启动另一个活动

And now you can just add the interface as an extra to the Intent which starts the other Activity:

Intent intent = new Intent(context, OtherActivity.class);
intent.putExtra("interface", inter);
startActivity(intent);

OtherActivity 中你可以获得来自 Intent 对象,如下所示:

And in the OtherActivity you can get the Object from the Intent like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    MyInterface inter = (MyInterface) intent.getSerializableExtra("interface");

    ...
}






编辑:



当您在活动中执行此类操作时,您正在创建匿名class:


When you do something like this in your Activity you are creating an anonymous class:

OnCreateListener inter = new OnCreateListener() {

    @Override
    public void onObjCreate() {
        Log.d("pltk", "dfgbfdgh");
    }
};

关于这样一个匿名类的事情是它们不是静态的,这意味着你仍然可以访问方法和嵌套此侦听器的类中的变量。在内部,原因是这个新类保留了对创建它的类的实例的引用,在您的情况下是封闭的 Activity 。这是一件好事,我们一直在使用 OnClickListener 等等。但在你的情况下这是一个问题,因为你想发送这个对象到另一个活动。对旧的 Activity 的内部引用使它不会被序列化,这是一件好事。如果您只是发送一个 Object ,就会因为垃圾收集器无法收集的所有旧引用而像疯了一样创建内存泄漏。

The thing about such an anonymous class is that they are not static, meaning you can still access methods and variables from the class in which this listener is nested. Internally the reason for this is that this new class keeps a reference to the instance of the class which created it, in your case the enclosing Activity. This is a great thing and we use it all the time for OnClickListener etc. But in your case it is a problem because you want to send this Object to another Activity. The internal reference to the old Activity keeps it from being serialised and that is a good thing. If you could just send an Object like that you would create memory leaks like crazy because of all the old references which the garbage collector cannot collect.

解决方案非常简单,您可以在自己的文件中定义类,也可以使类静态。静态在这种情况下意味着该类基本上被视为在它自己的单独文件中,因此无法访问封闭类的实例。

The solution is pretty simple, you can either define the class in its own file or you can make the class static. Static in this context means that the class is essentially treated like it were in it's own separate file and therefore cannot access the instance of the enclosing class.

所以总结一下你的内容必须做的是保持嵌套类并将其定义为静态如下:

So to summarise what you have to do is either keep the class nested and define it static like this:

public class YourActivity extends Activity {

    private static class OnCreateListenerImpl implements OnCreateListener {

        @Override
        public void onObjCreate() {
            Log.d("pltk", "dfgbfdgh");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_palatok);

        OnCreateListener inter = new OnCreateListenerImpl();
        Intent in = new Intent(Palatok.this, SecondActivity.class);
        in.putExtra("ob", inter);
        startActivity(in);
    }
}

或者你可以在单独的文件中移动实现:

Or you can move the implementation in its own separate file:

public class OnCreateListenerImpl implements OnCreateListener {

    @Override
    public void onObjCreate() {
        Log.d("pltk", "dfgbfdgh");
    }
}

虽然您之所以想要发送 OnCreateListener 到另一个活动仍然无法解决,这可以解决您的问题。

Although the reason why you would want to send an OnCreateListener to another Activity still eludes me, this should solve your problem.

我希望我能提供帮助,如果您有任何其他问题,请随时提出!

I hope I could help and if you have any further questions feel free to ask!

这篇关于Android:如何将接口从一个活动发送到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆