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

查看:29
本文介绍了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();
    }
}

主要活动:

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 中的一个方法
有这样的方法吗?

注意:我不想使用片段.

推荐答案

首先,这很糟糕:

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();
    }
}

这样更安全、更干净.

要将 Object 发送到另一个 Activity,请确保它实现了 Serializable,如下所示:

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

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

现在你可以将接口作为额外的接口添加到启动另一个 ActivityIntent 中:

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 获取 Object:

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");

    ...
}

<小时>

当您在 Activity 中执行类似操作时,您正在创建一个匿名类:


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 等.但在您的情况下,这是一个问题,因为您想将此 Object 发送到另一个 活动.对旧 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 发送到另一个 Activity 的原因仍然不明白,但这应该可以解决您的问题.

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天全站免登陆