简体   繁体   中英

For supporting different screen size why should i use fragment in android instead of activity

I am beginner to android..I am started new android project..for supporting different screen size..in fragment documentation they given to use fragment..but why cant i use activity in android..if i use activity or fragment..which i should i use in this both..please dont give link of activity or fragment..please anyone answer me..i dont know which to use?...i want about all documentation they given about activity and fragment but i dint understand which to use..below is the link i read about fragment..if i use activity i should do more codings?

https://developer.android.com/guide/components/fragments.html

In fact you can't use a Fragment alone, Fragments are inside the Activity. One point of using the Fragments for supporting different screen sizes is the ability to implement some views like a "Master/Detail" view. A Fragment, as its name says, is a part of a bigger controller "the Activity", its reference can be removed and it's cleaner than having a big massive Activity to handle all the states of a view.

So the use case is completely depends on your project and its User Interface. I'd be glad to help you if you give me more information about your project and its design.

I think you will need at least one activity. And then for better handling different device rotations and screen sizes you can use one or more fragments inside this activity.

I try to explain this with an example: You want to create a nice music player app which should look nice in portrait and landscape mode.

You split your app up into three fragments:

Here you can see how the app looks in portrait mode. The activity shows two fragments: The first fragment only consists of a listview. There the song titles are listed. On the bottom you can see the second fragment, which displays the song title of the current playing song and got a button for pausing the music.

片段1和2

When your user uses the music player app on a tablet in landscape mode you have more space for displaying stuff. Then the activity shows the list fragment (which also gets displayed in portrait mode) and it shows a third fragment which shows detailed information about the current playing song (eg the album image) and a progress bar.

片段1和3

By using fragments you only need to write the code for the list once.

Sweet and Simple thing, What i recommend is always use Fragments , But for Fragment you will require Activity .

  • Take it in this way , Activity is a Canvas on which you can put any number of Fragments . Whatever your UI is always use Fragment present on a activity if you want to show one screen even then also, So that you will always have Flexibilty to use all those cool things which fragments provides,maybe in future or in current. If you use activity it has limits, FOR EXAMPLE, LIKE in INSTAGRAM AT BOTTOM, It has FIVE OPTIONS, Suppose THOSE OPTIONS ARE ON A ACTIVITY AND BY CLICKING ON THEM YOU CAN SWITCH TO DIFFERENT FRAGMENTS .

For more info: Here is the most accepted answer for this topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM