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
2 answers

Google+ Login Not working properly on Android fragment

I am working with google+ login to my application and when i done it using a activity its work charm and after that i move my code into a fragment and after that when i try to login to google+ its not working i have to open the fragment activity 2…
11
votes
4 answers

Restore state of android's search view widget

Im having a search view in my action bar and i want to restore the query after orientation changed. How can I do this? Setting the query from the bundle using SearchView.setQuery(query, false); doesn't work as the searchview sets its query to "" as…
tobs
  • 699
  • 2
  • 8
  • 24
11
votes
5 answers

Cannot add multiple fragments to LinearLayout

I am using a LinearLayout with a vertical orientation to list fragments. I add fragments to the container programmatically like this: FragmentTransaction ft = fragmentManager.beginTransaction(); Fragment fragment1 = new…
ferpar1988
  • 596
  • 2
  • 5
  • 17
11
votes
4 answers

Android Fragment transition exit animation played above enter animation

I'm implementing Fragment transition animations. My exit animation is
Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101
11
votes
2 answers

Change z-order of Fragments during ongoing FragmentTransaction

Is there a way how to change the z-order in which are fragments displayed during an ongoing FragmentTransaction? I've an animation where both fragments overlaps each other and I would like to have the fragment which slides from the right (the second…
11
votes
2 answers

Fragment's onActivityResult not called after orientation change

Please note, this question is not a duplicate of the following: https://stackoverflow.com/questions/19006776/onactivityresult-not-working-with-fragments onActivityResult not working on fragments Also, another similar question was asked before, but…
Shade
  • 9,936
  • 5
  • 60
  • 85
11
votes
3 answers

Implementing DatePicker in Fragment

I'am using Fragment which is an instance of Fragment and not of DialogFragment. I did google most of the search result shows how to use DialogFragment to have DatePicker. which isn't working in my case because of type mismatch of Fragment and…
AndroidNewbie
  • 669
  • 3
  • 16
  • 23
11
votes
2 answers

android.support.v4.app.Fragment.setUserVisibleHint null pointer on app resuming

I am getting a crash on the resume of the app in the fragments code. I have never seen this crash myself but I have received crash reports back from users via TestFlight. I guess there is something that I am missing as the code works fine on most…
brad J
  • 542
  • 1
  • 11
  • 26
11
votes
3 answers

getParentFragment API 16

We all know getParentFragment of Fragment is introduced in API 17. So what if we want to get parent fragment in API 16 and below (Considering that I use native Fragment with support FragmentStatePagerAdapter and have no problem with nested…
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
11
votes
2 answers

Struggling with Youtube Player Support Fragment

Im trying to use the Youtube Player Support Fragment in a fragment but the app always crash (NullPointerException) and I have not been able to find any similar post to fix it. I have import import android.support.v4.app.Fragment so that should not…
11
votes
1 answer

android fragment inside dialog

I have an issue where I need to show a fragment in a android.app.Dialog here is the xml code
TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50
11
votes
3 answers

duplicated id with fragment

I'm trying to applicate drawernavigation (my first fragment is a map & the others are just some fragments with simple layouts).So it runs fine & I can navigate between my fragments but when I return to the first fragment which is a map I got a …
11
votes
5 answers

onClickListener does not work in fragment

I've got some problems with the onClicklistener in the fragment. If I click on the button nothing happens at all. I get neither a message from the onClicklistener in Logcat nor does a Toast appear on the screen, but I can't find the error in the…
reedts
  • 143
  • 1
  • 1
  • 8
11
votes
1 answer

Android: onActivityResult in fragment not called

Found numerous post regarding my issue but none of them worked out for me. I have a fragment from where I have to call startActivityForResult() on a textview click. From there, an activity will open up where user will fill some data and submit it to…
Nitish
  • 3,097
  • 13
  • 45
  • 80
11
votes
6 answers

Add fragment into listview item

I want to have a fragment for each item in a listview, because I want to separate some logic out. I am using a view holder for each item. If the view doesn't exist, I create a new fragment and add it into the container. holder.mMyFragment = new…
Fei Qu
  • 999
  • 2
  • 12
  • 26