爲什麼片段,何時使用片段而不是活動?

本文翻譯自:Why fragments, and when to use fragments instead of activities?

In Android API 11+, Google has released a new class called Fragment . 在Android API 11+中,Google發佈了一個名爲Fragment的新類。

In the videos, Google suggests that whenever possible ( link1 , link2 ), we should use fragments instead of activities, but they didn't explain exactly why. 在視頻中,谷歌建議儘可能( link1link2 ),我們應該使用片段而不是活動,但他們沒有解釋確切原因。

What's the purpose of fragments and some possible uses of them (other than some UI examples that can be easily be achieved by simple views/layouts)? 片段的目的是什麼?它們的一些可能的用途(除了一些可以通過簡單的視圖/佈局輕鬆實現的UI示例)?

My question is about fragments: 我的問題是片段:

  1. What are the purposes of using a fragment? 使用片段的目的是什麼?
  2. What are the advantages and disadvantages of using fragments compared to using activities/views/layouts? 與使用活動/視圖/佈局相比,使用片段有哪些優缺點?

Bonus questions: 獎金問題:

  1. Can you give some really interesting uses for fragments? 你能爲片段提供一些非常有趣的用途嗎? Things that Google didn't mention in their videos? 谷歌在他們的視頻中沒有提到的事情?
  2. What's the best way to communicate between fragments and the activities that contain them? 片段與包含片段的活動之間進行通信的最佳方式是什麼?
  3. What are the most important things to remember when you use fragments? 使用片段時要記住哪些最重要的事情? Any tips and warnings from your experience? 您的經驗提示和警告?

#1樓

參考:https://stackoom.com/question/hxrl/爲什麼片段-何時使用片段而不是活動


#2樓

Not sure what video(s) you are referring to, but I doubt they are saying you should use fragments instead of activities, because they are not directly interchangeable. 不確定你指的是哪些視頻,但我懷疑他們是說你應該使用片段而不是活動,因爲它們不能直接互換。 There is actually a fairly detailed entry in the Dev Guide, consider reading it for details. 在開發指南中實際上有一個相當詳細的條目 ,請考慮閱讀它以獲取詳細信息。

In short, fragments live inside activities, and each activity can host many fragments. 簡而言之,片段存在於活動內部,每個活動都可以容納許多片段。 Like activities, they have a specific lifecycle, unlike activities, they are not top-level application components. 與活動一樣,它們具有特定的生命週期,與活動不同,它們不是頂級應用程序組件。 Advantages of fragments include code reuse and modularity (eg, using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets). 片段的優點包括代碼重用和模塊化(例如,在許多活動中使用相同的列表視圖),包括構建多窗格界面的能力(主要用於平板電腦)。 The main disadvantage is (some) added complexity. 主要缺點是(某些)增加了複雜性。 You can generally achieve the same thing with (custom) views in a non-standard and less robust way. 通常,您可以使用非標準且不太穩健的方式使用(自定義)視圖實現相同的功能。


#3樓

#1 & #2 what are the purposes of using a fragment & what are the advantages and disadvantages of using fragments compared to using activities/views/layouts? #1和#2使用片段的目的是什麼?與使用活動/視圖/佈局相比,使用片段的優點和缺點是什麼?

Fragments are Android's solution to creating reusable user interfaces. 片段是Android創建可重用用戶界面的解決方案。 You can achieve some of the same things using activities and layouts (for example by using includes). 您可以使用活動和佈局(例如使用包含)來實現某些相同的功能。 However; 然而; fragments are wired in to the Android API, from HoneyComb, and up. 片段從HoneyComb連接到Android API。 Let me elaborate; 讓我詳細說明;

  • The ActionBar . ActionBar If you want tabs up there to navigate your app, you quickly see that ActionBar.TabListener interface gives you a FragmentTransaction as an input argument to the onTabSelected method. 如果您希望選項卡在那裏導航您的應用程序,您會很快看到ActionBar.TabListener接口爲您提供了FragmentTransaction作爲onTabSelected方法的輸入參數。 You could probably ignore this, and do something else and clever, but you'd be working against the API, not with it. 你可能會忽略這一點,做一些別的和聰明的事情,但是你會反對API而不是它。

  • The FragmentManager handles «back» for you in a very clever way. FragmentManager以非常聰明的方式爲您處理«返回»。 Back does not mean back to the last activity, like for regular activities. 返回並不意味着回到最後一個活動,就像常規活動一樣。 It means back to the previous fragment state. 這意味着回到之前的片段狀態。

  • You can use the cool ViewPager with a FragmentPagerAdapter to create swipe interfaces. 您可以將炫酷的ViewPagerFragmentPagerAdapter一起使用來創建滑動界面。 The FragmentPagerAdapter code is much cleaner than a regular adapter, and it controls instantiations of the individual fragments. FragmentPagerAdapter代碼比常規適配器更清晰,並且它控制各個片段的實例化。

  • Your life will be a lot easier if you use Fragments when you try to create applications for both phones and tablets. 如果在嘗試爲手機和平板電腦創建應用程序時使用Fragments,您的生活將會輕鬆許多。 Since the fragments are so tied in with the Honeycomb+ APIs, you will want to use them on phones as well to reuse code. 由於片段與Honeycomb + API緊密相關,因此您還需要在手機上使用它們以重用代碼。 That's where the compatibility library comes in handy. 這就是兼容性庫派上用場的地方。

  • You even could and should use fragments for apps meant for phones only. 您甚至可以而且應該將片段用於僅適用於手機的應用程序。 If you have portability in mind. 如果你有可攜帶性。 I use ActionBarSherlock and the compatibility libraries to create "ICS looking" apps, that look the same all the way back to version 1.6. 我使用ActionBarSherlock和兼容性庫來創建“ICS外觀”應用程序,這些應用程序看起來一直回到1.6版本。 You get the latest features like the ActionBar , with tabs, overflow, split action bar, viewpager etc. 您可以獲得ActionBar等最新功能,包括標籤,溢出,分割操作欄,viewpager等。

Bonus 2 獎金2

The best way to communicate between fragments are intents. 在片段之間進行通信的最佳方式是意圖。 When you press something in a Fragment you would typically call StartActivity() with data on it. 當您在片段中按某些內容時,通常會使用其上的數據調用StartActivity() The intent is passed on to all fragments of the activity you launch. 意圖將傳遞給您啓動的活動的所有片段。


#4樓

A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity which enable a more modular activity design. 片段是應用程序的用戶界面或行爲的一部分,可以放在Activity中,從而實現更模塊化的活動設計。 It will not be wrong if we say a fragment is a kind of subactivity. 如果我們說片段是一種子活動就不會錯。

Following are important points about a fragment: 以下是關於片段的重要觀點:

  1. A fragment has its own layout and its own behavior with its own lifecycle callbacks. 片段具有自己的佈局和自己的行爲以及自己的生命週期回調。

  2. You can add or remove fragments in an activity while the activity is running. 您可以在活動運行時在活動中添加或刪除片段。

  3. You can combine multiple fragments in a single activity to build a multi-pane UI. 您可以在單個活動中組合多個片段以構建多窗格UI。

  4. A fragment can be used in multiple activities. 片段可用於多個活動。

  5. The fragment life cycle is closely related to the lifecycle of its host activity. 片段生命週期與其宿主活動的生命週期密切相關。

  6. When the activity is paused, all the fragments available in the acivity will also be stopped. 當活動暫停時,活動中可用的所有片段也將停止。

  7. A fragment can implement a behavior that has no user interface component. 片段可以實現沒有用戶界面組件的行爲。

  8. Fragments were added to the Android API in Android 3 (Honeycomb) with API version 11. 使用API​​版本11將片段添加到Android 3 (Honeycomb)的Android API中。

For more details, please visit the official site, Fragments . 有關詳細信息,請訪問官方網站Fragments


#5樓

Fragments are of particular use in some cases like where we want to keep a navigation drawer in all our pages. 片段在某些情況下特別有用,例如我們想在所有頁面中保留導航抽屜。 You can inflate a frame layout with whatever fragment you want and still have access to the navigation drawer. 您可以使用您想要的任何片段爲框架佈局充氣,並且仍然可以訪問導航抽屜。

If you had used an activity, you would have had to keep the drawer in all activities which makes for redundant code. 如果您使用過某個活動,則必須將抽屜保留在所有可能產生冗餘代碼的活動中。 This is one interesting use of a fragment. 這是片段的一個有趣用法。

I'm new to Android and still think a fragment is helpful this way. 我是Android新手,仍然認爲片段有用這種方式。


#6樓

I know this was already discussed to death, but I'd like to add some more points: 我知道這已經討論過死亡,但我想補充一點:

  • Frags can be used to populate Menu s and can handle MenuItem clicks on their own. Frags可用於填充Menu ,並可自行處理MenuItem點擊。 Thus giving futher modulation options for your Activities. 從而爲您的活動提供更多調製選項。 You can do ContextualActionBar stuff and so on without your Activity knowing about it and can basically decouple it from the basic stuff your Activity handles (Navigation/Settings/About). 你可以在沒有你的活動知道的情況下做ContextualActionBar等等,並且基本上可以將它與你的Activity處理的基本內容(導航/設置/關於)分開。

  • A parent Frag with child Frags can give you further options to modulize your components. 帶有子Frags的父Frag可以爲您提供模塊化組件的更多選項。 Eg you can easily swap Frags around, put new Frags inside a Pager or remove them, rearrange them. 例如,您可以輕鬆地交換Frags,將新的Frags放入尋呼機或移除它們,重新排列它們。 All without your Activity knowing anything about it just focusing on the higher level stuff. 所有沒有你的活動知道任何事情只關注更高層次的東西。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章