1

I am developing a forum reader, which I expect that it will be run on phone only, not tablet.

The application originally have 3 views, and each is assigned with an Activity. For example, if the user select a board on "Board selection screen", an Activity will start, and show the thread list of the selected board to the user.

Recently I have learned how to use fragment in an Android application. therefore I decided to convert my application from an Activity based apps to a Fragment based apps.

Although the apps works as fine as usual after the change, I encountered a problem in saving the state of the fragment. I failed to find the event to let me save the state of fragment (When I am switching from A fragment to B fragment). The most likely event is onSaveInstanceState(Bundle), but according to the API guide, it will only be called when an activity is about to be stopped, and it is not my case.

Actually I have tried to save the state in getArgument() in onDestroyView(). Nevertheless, according to the API guide, the values in getArgument() should only be used for instancing a fragment. Although everything work normal when I am saving the state in getArgument(), I believe that I am doing it wrong.

I have tried to do some googling about Fragment. I found that most of the artist suggest that Fragment is useful when an application is expected to run on both phone and tablet. And I don't find any artist that is talking about how to do screen switching between few Fragment within an Activity. I started to doubt that Fragment are only useful for supporting different device, but not for screen switching.

Sorry for the above junks. My questions are, does Fragment is not very useful when I am developing an application which for phone only? Should I change my application back to an Activity-based apps if I don't expect this to run on tablet?

4

3 回答 3

1

Fragments 擅长的另一件事是它们对 Dialogs 有好处。旋转手机时,基于片段的对话框没有通常使用 showDialog() 时遇到的问题

于 2013-09-14T19:04:33.410 回答
0

如果我们谈论“只是工作”,您可以继续使用片段或只是正常的活动和布局。然而,在为平板电脑等其他设备开发时,除了可扩展性之外,Fragment 提供的功能是它为您提供了更多的代码分离,这对于应用程序的维护非常有用。

于 2013-09-14T18:50:07.267 回答
0

不,你应该坚持使用 Fragments。它们非常多才多艺,如果您决定重新安排事物,它们将使您的生活更轻松。至于保存状态,您只需要覆盖onSaveInstanceState()您的Fragment(而不是Activity)。在片段之间切换时绝对会调用它——我一直使用它。

于 2013-09-14T18:49:27.403 回答