简体   繁体   English

Android:如何将回调传递给活动?

[英]Android: How do you pass a callback to an activity?

I am creating a library. 我正在创建一个库。 It creates an activity, starts it, and finishes it after a button is pressed. 它会创建一个活动,启动它,并在按下按钮后完成它。

After the button is pressed, I'd like to execute some client code through an interface. 按下按钮后,我想通过一个接口执行一些客户端代码。

That is, users of this library pass in an implementation, and I simply execute it. 也就是说,这个库的用户传入一个实现,我只是执行它。

The analogous iOS code would be: 类似的iOS代码将是:

MyViewController *vc = [[MyViewController alloc] init];
vc.callOnComplete = ...
[self presentViewController:vc animated:YES completion:nil];

The desired code is: 所需的代码是:

public interface SaveStuff {
    void save();
}

In my library's Activity: 在我的图书馆的活动中:

private SaveStuff saveStuff;
public void onButtonClicked(View v) {
    saveStuff.save() // client passes in saveStuff obj
    finish();
}

I've tried serialization. 我尝试过序列化。 I made a class that implemented the interface and also implemented Serializable , and tried passing that in and extracting it with getSerializableExtra . 我创建了一个实现接口的类,并且还实现了Serializable ,并尝试将其传递并使用getSerializableExtra将其getSerializableExtra I end up getting a non serializable exception. 我最终获得了一个非序列化的异常。 So far as I know, this is probably not a viable solution, as it would mean that clients must use only serializable classes in their implementations. 据我所知,这可能不是一个可行的解决方案,因为它意味着客户端必须在其实现中仅使用可序列化的类。 Correct me if I am wrong on this. 如果我错了,请纠正我。 In my trials, the client activity was showing up in the non serializable exception. 在我的试验中,客户端活动显示在非序列化异常中。

I've tried using a static reference. 我尝试过使用静态引用。 Basically global state/singleton. 基本上是全球州/单身人士。 As it turns out, according to a variety of answers such as this one: Java Static Variable becomes null , I cannot rely on any static state, and any solutions that do work with static state are just working coincidently. 事实证明,根据诸如此类的各种答案: Java静态变量变为空 ,我不能依赖任何静态状态,并且任何与静态一起工作的解决方案只是巧合。

I haven't tried simply starting the activity with startActivityForResult , because that would require me to cast the context we are accepting as a parameter to an Activity. 我没有尝试使用startActivityForResult简单地启动活动,因为这需要我将我们接受的上下文作为参数投射到Activity。 The activity is started inside the library with this: 该活动开始这个库里面

private void startNewActivity(int newVersion) {
    Intent i = new Intent(context, MyActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(i); // context variable is passed in by the client
}

As you can see, context is passed in by the client. 如您所见,客户端传递上下文。 I could perhaps cast the context to an Activity in order to call startActivityForResult , but I don't want to do this, as I have read that is not always safe. 我可以将上下文startActivityForResult为一个Activity来调用startActivityForResult ,但是我不想这样做,因为我已经读过并不总是安全的。 I could require that an Activity be passed in. This is perhaps a reasonable requirement, but I don't want to go there just yet. 我可以要求传入一个Activity 。这可能是一个合理的要求,但我不想去那里。

I've looked at the existing questions and something I haven't tried is using broadcasts. 我看过现有的问题,而我没有尝试的是使用广播。 Correct me if I am thinking about this incorrectly, but the best I could do is have the object that holds the reference to client interface implementation be a broadcast receiver, and then send a broadcast in the activity that I create. 如果我错误地想到这一点,请纠正我,但我能做的最好的事情是将对客户端接口实现的引用保持为广播接收器,然后在我创建的活动中发送广播。 I am worried that the receiver will not persist, however. 但是我担心接收器不会持续存在。 What happens if my library shows the user the activity, but then the user kills the app before pressing the button? 如果我的库向用户显示活动,但用户在按下按钮之前杀死了应用程序,会发生什么? The reference to the client code was held by the previous activity. 客户端代码的引用由上一个活动保存。 Will nothing happen upon pressing the button? 按下按钮什么都不会发生?

Should I just broadcast a message, and leave it up to the client to handle these cases? 我应该只是广播一条消息,并将其留给客户处理这些案件吗? How would the client handle this? 客户如何处理这个? Register something on app start up/shutdown? 在app启动/关闭时注册一些东西? Do I have to force the user to do this to use my library? 我是否必须强制用户执行此操作才能使用我的库? Is it reasonable? 这合理吗?

Update: 更新:

As it turns out, we are just going to have the activity in the library access a singleton. 事实证明,我们只是让库中的活动访问单例。 To avoid all the issues with state being wiped based on activities being killed and the like, users of this library will be required to run a 'setup' method on the singleton, and pass in the callback obj there. 为了避免基于被杀死的活动等被擦除状态的所有问题,该库的用户将被要求在单例上运行'setup'方法,并在那里传递回调obj。 They will run this on some sort of base Activity's onResume. 他们将在某种基础Activity的onResume上运行它。 I'll wait a little before answering self-answering this question to see if there any alternatives. 在回答这个问题的自我回答之前,我会稍等一下,看看是否还有其他选择。

Why don't you use same process for android as well ie 1) make an interface 2) declare a public variable of interface in your activity. 你为什么不为android使用相同的过程,即1)创建一个接口2)在你的活动中声明一个接口的公共变量。 3) user will initialize that interface when start to launch your activity. 3)用户将在开始启动您的活动时初始化该界面。 4) use the interface variable to call the method. 4)使用接口变量来调用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将活动中的值传递给 Android Studio 中的 class? - How do you pass values from an activity to a class in Android Studio? 如何将数据/参数传递给 Android 中的另一个活动 - How do you pass data/parameters to another activity in Android Android-如何将Textview值从Activity-A传递到适配器 - Android - How do you pass textview values from Activity-A to an adapter 如何将 Dictionary 传递给 Android Studio 中的新活动? - How can you pass Dictionary to a new Activity in Android Studio? 你如何在 android 中创建一个按钮来打开一个新的活动,这个活动不仅仅是一个默认活动,它是一个自定义活动 - How do you make a button in android that opens a new activity and this activity is not just a default activity its a custom one 您如何在Android自动化活动中触发Java KeyEvent? - How do you fire a Java KeyEvent inside an Android automation activity? 在 android 中,您如何搜索互联网并在活动中返回结果? - In android how do you search the internet and return the results in an activity? Android:你如何点击其他活动? - Android: How do you go to another activity on click? 如何在 Android Studio 中开始新的 window 活动? - How do you start a new window activity in Android Studio? 如何将用户名传递给 Android Studio 中的另一个活动 - How do I pass an Username to another Activity in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM