在活动中运行活动?
我正在为 Android 开发一个库。该库包含一个自定义视图。我希望能够在为活动调用 onConfigurationChanged()
时从我的库中进行检测。
我的第一个想法是将 startActivity()
与我自己的仅实现 onConfigurationChanged()
的活动一起使用,但不幸的是,这会在应用程序顶部启动一个新活动。是否可以在“后台”运行活动?
也许我的方法错误?您知道我如何实现这一目标吗?
I am developing a library for Android. This lib consist of a custom view. I'd like to be able to detect from my lib when onConfigurationChanged()
is called for the activity.
My first thought was to use startActivity()
with my own activity that only implement onConfigurationChanged()
, but unfortunatly this start a new activity on top of the application. Is it possible to run an activity in "background"?
Maybe I am having the wrong approach? Do you have any idea of how I can achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以我试图解决这个问题的方式并不好。
我在我的库中添加了一个方法
onConfigurationChanged()
。这样,使用它的人就必须从他们的活动中调用这个方法。问题解决了!
So the way I was trying to solve it wasn't googd.
I added a method
onConfigurationChanged()
to my lib. That way, people using it will have to call this method from their activity.Problem solved!
我遇到了类似的问题 - 一个自定义图库创建了自己的对话框并需要跟踪配置更改(因为当方向更改时它会泄漏其视图)。
为了解决这个问题,当检测到这样的变化时,必须调用dialog.dismiss()。我已经需要主活动来调用自定义视图的一些方法,例如 onContextItemSelected 和 onActivityResult。
我想要对用户更透明的东西。在这种特殊情况下,我通过从
View
(而不是从Activity
)调用onSaveInstanceState
中的 dismiss 方法来实现此目的,并保持调用者活动完整无缺,无需从自定义视图中调用任何更多方法。I had a similar problem - a custom Gallery that created its own dialog and needed to track configuration changes (for it leaked its views when orientation changed).
To solve this, dialog.dismiss() must had to be called when such a change was detected. I already needed main activity to call some methods of the custom view, onContextItemSelected and onActivityResult for instance.
I would like something more transparent to users. In this particular case, I achieved this by calling the dismiss method from the
onSaveInstanceState
fromView
(not fromActivity
), and keep the caller activity intact without having to call any more methods from the custom view.您需要在清单中定义该活动处理 onConfiguratoinChange,然后在代码中定义它。您可以在此处查看 Google 文档以获取示例。 http://developer.android.com/guide/topics/resources/runtime -changes.html
You need to define in your manifest that the activity handles the onConfiguratoinChange and then define it in your code. You can check out the Google docs here for an example. http://developer.android.com/guide/topics/resources/runtime-changes.html