应用程序在调用新活动时崩溃
这对您来说可能不是一个新问题 - 我有我的主要活动,我想在另一个活动中调用一个方法。现在,如果我这样做,我的应用程序将不会响应并被关闭。
我听说有一些特殊的方法可以调用其他活动类中的方法?
this might not be a new question to you - I have my main activity and I would like to call a method in another activity. Now if I do this I get my application not responding and closed.
I hear there is some special way to call methods in other activity classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这通常是一个坏主意,原因是您无法控制非活动活动的生命周期。您可以调用一个方法,并发现您正在调用的活动已被销毁以回收资源。
更好的选择是创建一个实用程序类,该类可以为多个活动提供功能,或者将在应用程序的整个生命周期中必须保留的内容放入应用程序类或共享首选项中。
如果您尝试做的只是将信息传递给新活动或从另一个活动获取信息,您可能希望将该信息打包到意图中,或者再次将其放置在共享首选项中。
This is generally a bad idea, the reason why is because you have no control of the life-cycle of an activity that is not the active Activity. You could call a method and find that the activity you are calling has been destroyed to reclaim resources.
A better option would be to create a Utility class that can provide functions to multiple activities, or place things that must remain throughout the life-cycle of your application into the application class, or shared preferences.
If all your trying to do is pass information to a new activity or get information from another one, you may want to package that information into the intent, or again place it in shared preferences.
只需创建另一个类并将该方法放置在该类中。然后从两个活动进行访问。
Just create another class and place that method in that class.Then access from both activity.