片段最佳实践
我的 Android 3.1 应用程序中有一个内容提供程序,并且正在使用具有 content://my.package/xxx Uris 和 Intent.ACTION_VIEW 操作的 Intents 调用 startActivity()。在我的 andoroid-manifest.xml 中,我设置了内容提供商的 mime 类型,以便我的 Activity 获取意图。一切都很好。
这是我的情况
- 当绘制带有片段的 Activity 时,所有片段都会收到其生命周期事件。
- 我不希望在意图上重新绘制/更新所有片段。因为传入的 URI 通常只是传递到我的内容提供程序上的查询方法。意图通常仅与特定片段相关。
如何阻止与当前 Intent 无关的片段发生事情?
I have a content provider in my Android 3.1 Application, and are calling startActivity() with Intents that has content://my.package/xxx Uris, and Intent.ACTION_VIEW actions. In my andoroid-manifest.xml I have my content provider's mime-types set up so that my Activity picks up the intents. Evert thing works fine.
Here is my situation
- When an Activity with fragments is drawn, all fragments receive their life cycle events.
- I don't want all fragments to be redrawn/updated on the Intent. Because the incoming URI is usually just passed along to a the query method on my content provider. The intent is usually only relevant to a specific fragment.
How do I stop things from happening in fragments that has nothing to do with the current Intent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以发布一些与您的问题相关的代码吗?特别是,yoru Activity 类代码中处理片段管理的部分以及布局的 .xml 文件?
如果您直接在布局的
.xml
文件中声明片段,则每次都会实例化片段。如果您不希望出现此行为,请仅以编程方式实例化和添加片段(在活动代码中将FragmentTransacations
与FragmentManager
结合使用,并将其从 .xml 声明中删除。Can you post some code that's relevant to your problem? In particular, the portion of yoru Activity class code that deals with Fragment management, and the layout's .xml file?
If you are declaring the fragments directly in the layout's
.xml
file, the fragments will be instantiated every time. If you do not want this behavior, instantiate and add fragments only programmatically (usingFragmentTransacations
with theFragmentManager
in your activity code, and remove them from the .xml declaration.