Questions tagged [android-fragments]

Fragments represent reusable behaviors or portions of the user interface in an Android app.

A Fragment represents a behavior or a portion of user interface in an Activity. and reuse a fragment in multiple activities.

Fragments were first introduced in Android 3.0 (API 11). However, the Support V4 Library back-ported fragments to every version of Android from 1.6 (API 4) onwards.

Here are the important things to understand about fragments:

  • A Fragment is a combination of an XML layout file and a java class much like an Activity.
  • Using the support library, fragments are supported back to all relevant Android versions.
  • Fragments encapsulate views and logic so that it is easier to reuse within activities.
  • Fragments are standalone components that can contain views, events and logic.
  • You can combine multiple fragments in a single activity to build a multi-pane UI.

Usage of Fragments in different environments

  • You can add fragments to your app directly with XML or through the FragmentManager in Java.
  • The FragmentManager is responsible for all runtime management of fragments including adding, removing, hiding, showing, or otherwise navigating between fragments. The fragment manager is also responsible for finding fragments within an activity.

The ApiDemos sample application present in the SDK provides runnable fragment examples and source code.

You can find more information in:

Tag Usage:

45668 questions
11
votes
1 answer

Fragment.onCreateView has null container

The following is running on an Android 1.6 so I'm using the compatibility package for fragments. In the following TestFragment is a static nested class: public class FragmentTestActivity extends FragmentActivity { @Override public void…
Roland
  • 7,525
  • 13
  • 61
  • 124
10
votes
6 answers

Can't remove a fragment from FrameLayout?

I have a layout for landscape mode that shows a ListView on the left and a FrameLayout on the right. When an item is selected from the list another fragment is added to the FrameLayout MyFragment myFragment = (MyFragment)…
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
10
votes
1 answer

Android fragments and its influence on the performance

I have an Activity with many, many views (200+) and now I'm separating it into fragments in order to decouple my code a bit. The question is - how does it influence on the performance? Let's say I have N views hierarchically layed out in one layout…
Fenix Voltres
  • 3,448
  • 1
  • 29
  • 36
10
votes
4 answers

Navigate among fragments within one activity

I would like to use one Activity which holds several fragments and navigate among the fragments. For example, in the activity, there is a list view which is a fragment, when user select one item from the list, the view will navigate to another…
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
10
votes
2 answers

ViewFlipper vs Fragments

I have an Activity with a ViewFlipper that flips between a bunch of views (pages) with my data. I am considering of using the fragments API to switch between my views. What are the benefits of doing so? Could I see a performance gain by using…
dnkoutso
  • 6,041
  • 4
  • 37
  • 58
10
votes
2 answers

How do I restore a previously displayed Fragment?

I'm using the compatibility package v4 in my project and I'm having an issue with keeping a Fragment around after it's removed from view. I have my Activity displaying 2 fragments...a menu frame on the left, content pane on the right. The menu frame…
Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
10
votes
2 answers

How to run or start application inside the fragment of Android?

I want to launch any existing app of device inside a fragment. Can anyone please help me how can I do this. Thanks & BR, Pawan
Pawan
  • 1,503
  • 2
  • 19
  • 28
10
votes
0 answers
10
votes
2 answers

Android ListView in Fragment

I am trying to create a list of items which contains an image and some description for the image in each individual. After which, the list will be place in a fragment inside the application. Can anyone guide me to create it? I am not too sure how…
10
votes
3 answers

Highlighting a menu item in system settings

Trying to bring up an unaswered question I found here - How to highlight android setting app menu item? As seen in this video https://www.youtube.com/watch?v=eHXBc5Mmsqs The "Power Shade" menu item is being highlighted once you enter the screen. I…
10
votes
2 answers

ViewPager2 not able to dynamically add remove fragment

Removing/Adding fragments at index results in unexpected behaviour in Viewpager2. This was not possible with ViewPager but expected to work with Viewpager2. It causes duplicate fragments and out of sync TabLayout. Here is a demo project which…
10
votes
2 answers

Duplicate id, tag null, with another fragment for androidx.navigation.fragment.NavHostFragment

Issue I am trying to add a fragment in activity, along with Navigation. But I see the following error java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.myapp/com.app.myapp.HomeMainActivity}: android.view.InflateException:…
Darshn
  • 1,512
  • 1
  • 23
  • 31
10
votes
5 answers

Set peek height programmatically in BottomSheetDialogFragment

I have a BottomSheetDialogFragment that I want to be able to show on any screen. I've spent the day trying to programmatically change the peek height of the sheet, but nothing seems to be changing it. Here is my layout, bottom_sheet.xml:
10
votes
3 answers

Vertical ViewPager2 with RecyclerView Scrolling Issue

I'm using a ViewPager2 with two fragments in a vertical orientation. When the user swipes down to the second fragment, there is a RecyclerView that scrolls content in the same vertical direction. The issue is that when I scroll the contents of the…
10
votes
4 answers

Android: What's wrong with my fragment transition animation?

I just need plain slide in and slide out animation for Fragment transition, below is my code: slide_in_left.xml:
Hao Zhe XU
  • 101
  • 1
  • 1
  • 3
1 2 3
99
100