简体   繁体   English

从子活动调用父活动中的函数-Android

[英]Call function in parent Activity from child Activity - Android

How can I call function (with parameters) in parent activity from child activity in Android. 如何从Android的子活动中调用父活动中的函数(带有参数)。 I iOS is simple with delegate and protocols, but in Android I found that can be a problem if the screen is rotated. 我的iOS很简单,使用委托和协议,但是在Android中,我发现如果旋转屏幕会出现问题。

I also do not want to use startActivityForResult because I do not want to close the child Activity. 我也不想使用startActivityForResult,因为我不想关闭子Activity。

ADDED: I am creating a library so other developers can attach it to their project. 添加:我正在创建一个库,以便其他开发人员可以将其附加到他们的项目中。 In developers activity they put button that shows my(by my I mean from library) Activity and when something is done in my activity I instantly need to inform the main project(activity) that something happened (via function or something), but I do not want to close my activity yet. 在开发人员活动中,他们放置了一个按钮,该按钮显示我的活动(我的意思是从库中获得),当我的活动完成时,我需要立即通知主项目(活动)发生了某些事情(通过函数或其他方式),但是我确实不想结束我的活动。 I also should not change their code a lot and it should be easy to include to their code. 我也不应该对他们的代码进行太多更改,并且应该容易地将其包含在他们的代码中。

The point is that once you started a child activity, Android might decide to close your parent activity at any time, if system requires more memory. 关键是,一旦您启动了子活动,如果系统需要更多内存,Android可能会决定随时关闭父活动。 This means you cannot rely on the fact your parent activity still runs. 这意味着您不能依靠您的父活动仍在运行的事实。 That is why you cannot use static variable approach either, because parent activity might not be there already. 这就是为什么您也不能使用静态变量方法的原因,因为父活动可能还不存在。

If you want your app to run reliable in any situation you have to use startActivityForResult and close child activity. 如果希望您的应用在任何情况下都能可靠运行,则必须使用startActivityForResult并关闭子活动。

If this is not acceptable for you because of some reasons, you might share more details and we will try to find an appropriate solution for you in terms of Android concepts. 如果由于某些原因您对此无法接受,则您可能会分享更多详细信息,我们将尝试根据Android概念为您找到合适的解决方案。

ADDED: I am creating a library... 添加:我正在创建一个图书馆...

As already mentioned, you cannot control whether your parent activity gets closed or not as long you started a new child activity. 如前所述,只要启动新的子活动,就无法控制父活动是否关闭。 Android can kill your parent activity at any time and you need to be prepared to handle this situation properly. Android可以随时终止您的家长活动,因此您需要做好适当处理这种情况的准备。

To solve your problem you need a place, which is shared between parent and child activities. 要解决您的问题,您需要一个地方,该地方在父母和孩子的活动之间共享。 A sequence will be like this. 一个序列将是这样的。 Parent activity starts a child activity and goes into background. 家长活动开始孩子活动并进入背景。 User changes something in child activity and it stores changes (as data) into the shared place. 用户更改子活动中的某些内容,并将更改(作为数据)存储到共享位置。 At this step it doesn't matter whether parent activity still running or now. 在此步骤中,父活动是否仍在运行或现在都无关紧要。 Once parent activity is visible again, if must read data stored by child activity, and update itself accordingly. 一旦父活动再次可见,则必须读取子活动存储的数据,并相应地进行更新。 You can use onStart() method for this. 您可以为此使用onStart()方法。

Now about that shared place. 现在关于那个共享的地方。 I would discourage you from using a static variables. 我不鼓励您使用静态变量。 Instead you could use Application object (it's a singleton) or shared preferences (they are also shared). 相反,您可以使用Application对象(这是一个单例)或共享首选项(它们也是共享的)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM