使用Kotlin从另一个人完成android活动 [英] Finish android activity from another with Kotlin

查看:111
本文介绍了使用Kotlin从另一个人完成android活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Kotlin从另一个(android)完成一项活动.我知道使用Java执行此操作的方法是使用以下代码( https://stackoverflow.com/a/10379275/7280257)

在第一次活动中:

BroadcastReceiver broadcast_reciever = new BroadcastReceiver() {

    @Override
    public void onReceive(Context arg0, Intent intent) {
        String action = intent.getAction();
        if (action.equals("finish_activity")) {
            finish();
            // DO WHATEVER YOU WANT.
        }
    }
};
registerReceiver(broadcast_reciever, new IntentFilter("finish_activity"));

关于其他活动:

Intent intent = new Intent("finish_activity");
sendBroadcast(intent);

由于某种原因,将Java活动转换为kotlin并不会提供有效的输出,如果有人可以使用 kotlin 为我提供正确的语法来正确执行此操作,

kotlin输出(第一个活动) [确定] :

val broadcast_reciever = object : BroadcastReceiver() {

    override fun onReceive(arg0: Context, intent: Intent) {
        val action = intent.action
        if (action == "finish_activity") {
            finish()
            // DO WHATEVER YOU WANT.
        }
    }
}
registerReceiver(broadcast_reciever, IntentFilter("finish_activity"))

kotlin输出(第二项活动) [确定]

            val intent = Intent("finish_activity")
            sendBroadcast(intent)

错误: http://i.imgur.com/qaQ2YHv.png

修复:代码显示正确,只需将其放置在onCreate函数内

解决方案

出现错误Expecting member declaration的原因是您在类中编写了一条语句(函数调用).在该范围内,需要声明(函数,内部类).

您必须将语句放置在函数中(然后从某个地方调用这些语句)才能执行它们.

I'm trying to finish an activity from another (android) with kotlin. I know the wat to do it with java is with the following code (https://stackoverflow.com/a/10379275/7280257)

at the first activity:

BroadcastReceiver broadcast_reciever = new BroadcastReceiver() {

    @Override
    public void onReceive(Context arg0, Intent intent) {
        String action = intent.getAction();
        if (action.equals("finish_activity")) {
            finish();
            // DO WHATEVER YOU WANT.
        }
    }
};
registerReceiver(broadcast_reciever, new IntentFilter("finish_activity"));

On the other activity:

Intent intent = new Intent("finish_activity");
sendBroadcast(intent);

For some reason converting the java activity to kotlin doesn't give a valid output, if someone could give me the correct syntax to do it properly with kotlin I will appreciate it

kotlin output (first activity) [OK]:

val broadcast_reciever = object : BroadcastReceiver() {

    override fun onReceive(arg0: Context, intent: Intent) {
        val action = intent.action
        if (action == "finish_activity") {
            finish()
            // DO WHATEVER YOU WANT.
        }
    }
}
registerReceiver(broadcast_reciever, IntentFilter("finish_activity"))

kotlin output (2nd activity) [OK]

            val intent = Intent("finish_activity")
            sendBroadcast(intent)

ERROR: http://i.imgur.com/qaQ2YHv.png

FIX: THE CODE SHOWN IS RIGHT, YOU JUST NEED TO PLACE IT INSIDE THE onCreate FUNCTION

解决方案

The error Expecting member declaration is there because you wrote a statement (the function call) inside a class. In that scope, declarations (functions, inner classes) are expected.

You have to place your statements inside functions (and then call those from somewhere) in order for them to be executed.

这篇关于使用Kotlin从另一个人完成android活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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