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

findFragmentByTag() always return null - Android

In my application i have one main activity and several fragments. when user click the back button fragments pops one by one. I want to identify which fragment currently in the back stack. So use to identify fragments by fragment tag name. i used…
user3800832
  • 421
  • 2
  • 8
  • 22
11
votes
5 answers

Setting a subtitle on my Toolbar from a fragment

I am having some trouble setting the sub-title in my Toolbar from my fragment. I keep getting a Null Pointer Exception at the setSubTitle. @Override public void onActivityCreated(Bundle savedInstanceState) { …
11
votes
2 answers

Fragments not destroyed when recreate() activity

I have a support library fragment that sends a network call, and recreate() the parent activity when it receives a specific network response. I can see that the activity does get recreated as I can see the onCreate() and onDestroy() are called. But…
Cash Lo
  • 1,052
  • 1
  • 8
  • 20
11
votes
4 answers

Call parent fragment method from dialog fragment

I have a parent fragment, within this. Upon a button click, a child dialog fragment is getting created. Now I would like to know how to call parent fragment function from child dialog fragment. Here is the sample code…
Naruto
  • 9,476
  • 37
  • 118
  • 201
11
votes
1 answer

Change statusbar color on Fragment change [Android Lollipop]

I'm wondering how to change the StatusBar color dependent on the current active Fragment (on 5.0 Lollipop). Currently, I change the ActionBar color dependent on the Fragment I am in, but now I want the StatusBar color to change as well - in order to…
11
votes
3 answers

Is there any point of an Activity with one fragment?

All the reasons I can find for using Fragments in Android activities have to do with having the ability to display multiple classes/view in the same screen, encapsulating multiple logical components, etc. Considering all this, it seems, fragments…
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
11
votes
3 answers

Removing fragments from FragmentStatePagerAdapter

UPDATE After some major fighting with this problem and help from SO users I managed to solve it. This is how my clearProgressUpTo looks like now. public void clearProgressUpTo(int progress) { boolean deleted = false; for (int i =…
JakubW
  • 1,101
  • 1
  • 20
  • 37
11
votes
1 answer

Fragments in ViewPager not loaded when the containing Fragment is recreated

I have a Fragment MyFragment vith a ViewPager containing more Fragment. It works properly the first time I load MyFragment, but if I go back and recreate it, the Fragments in the ViewPager are not shown because the method…
jul
  • 36,404
  • 64
  • 191
  • 318
11
votes
11 answers

Exit app when press Back button in activity with multiple fragments in Navigation Drawer

I'm working on an app that need Navigation Drawer. This app has 1 MainActivity and about 10 fragments on it. When I click the Navigation Drawer icon in MainActivity it will show the 10 fragments so I can select each of them. I choose fragment A,…
0xh8h
  • 3,271
  • 4
  • 34
  • 55
11
votes
4 answers

How to know if a Fragment already called onCreateView()

We all know that when using ViewPager with Fragment and FragmentPagerAdapter we get 3 Fragment loaded: the visible one, and both on each of its sides. So, if I have 7 Fragments and I'm iterating through them to see which 3 of them are the ones that…
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
11
votes
5 answers

Going from 3rd to 1st fragment in the backstack: Android

I have a sequence of event via which i have added three fragments to the backstack, one by one. Each of these fragments covers the full screen of the activity. I have stored the is returned from the commit of Frag1. Now in Frag3, based on a…
Sunny
  • 7,444
  • 22
  • 63
  • 104
11
votes
4 answers

Does Fragment.getArguments() return passed arguments after configuration changes?

Sorry for my English Should I save and restore arguments (returned by getArguments()) during configuration changes via outState \ savedInstanceState ? Or getArguments() always returns passed arguments even after configuration changes ?
Leonid Semyonov
  • 473
  • 2
  • 6
  • 18
11
votes
2 answers

avoid reloading on back pressed in fragments

I am making an app for car finding.I am showing result in gridview and also have a sliding menu on right hand side(for search filter).I have made this whole app with the help of fragments.In the gridview I have a load more footer at bottom, when I…
userAndroid
  • 586
  • 2
  • 9
  • 26
11
votes
2 answers

How to update Android fragment from activity?

I want to periodically update a fragment's display based on data I download from the Internet. I have created a Timer and Runnable to periodically retrieve this data as well as a method within the fragment to update it, but I cannot seem to figure…
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
11
votes
7 answers

how to remove fragment layout in beginning of creating a new app in ADT?

After the latest update of the Android Developer Tools there is a problem I faced whenever I create a new project. It forces me to create a fragment_layout.xml as well. So in the hello world program there is too much of code. But in previous one…
Rajan Maurya
  • 624
  • 1
  • 12
  • 24