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

How to implement a swipe-gesture between Fragments?

I would like to implement in my android app that I'm developing with the Android studio ide. The swipe between the different tabs (fragment). I tried searching online and on StackOverflow but I could not quite understand. My action bar is the…
lillogoal
  • 203
  • 1
  • 3
  • 11
11
votes
3 answers

Fragment's OnClickListener called after onDestroyView

I have an issue where ListFragment.onListItemClick is called after onDestroyView. I'm getting lots of error reports in the field (10-20 per day of ~1000 active users), but the only way I found to reproduce it is to hammer the back button while…
rockgecko
  • 4,127
  • 2
  • 18
  • 31
11
votes
1 answer

implement AsyncTask in Fragment android

I've an activity which output json data from as a list. But now I want to implement it in a fragment. In this fragment I want to view it as gridview. And both files works fine. but when I tried to implement AsyncTask I gets several redflags as…
11
votes
1 answer

Fragments restore state on orientation changed

I have to implement "standart" fragments navigation in my app (please see link). The issue is when device is in portrait mode, there should be shown only 1 fragment, and when it is rotated to landscape mode, 2 fragments should be shown. I tried to…
11
votes
4 answers

getActivity().findViewById returns null, called from fragment onActivityCreated

I am following the contacts provider lesson on retrieving contacts and displaying them using fragments. For reference, I have set the API level to 16 (Android 4.1). I have mostly followed this tutorial to the letter, with a few notable exceptions.…
user257111
11
votes
5 answers

DrawerLayout - Exception dispatching input event

I recently started using the DrawerLayout in a new project about a week ago and it's all working fine. Apart from the odd times when swiping it in and out with my finger it'll crash. If I use the ActionBar Up/Home MenuItem though, it'll work fine…
11
votes
3 answers

Change viewpager fragment by a buttonClick

I'm trying to change the viewpager fragment by clicking on a button. I have 5 fragments, each fragment has it's own xml file (frag1.xml, frag2.xml, and so on). Every fragment has it's 5 buttons that should go to other pages of the viewpager. But the…
mXX
  • 3,595
  • 12
  • 44
  • 61
11
votes
2 answers

ViewPager + Adapter in Fragment => laggy swiping

I have a ViewPager with some fragments. Each fragment has a ListView in a SlidingDrawer (=invisible before swiping) with an ArrayAdapter. Adapter is set on onCreateView(), that slows down swiping, because 30 list items have to load each time I…
metinkale38
  • 718
  • 3
  • 10
  • 28
11
votes
1 answer

addToBackStack doesn't work, closes the activity instead of popping back fragments

I have a probleam and I can't find a solution anywhere. My app doesn't return to previous fragment when I press the back button, instead closes the activity. I have an activity that displays 4 fragment: [1],[2],[3],[4]. I can switch between the…
JoP
  • 555
  • 8
  • 18
11
votes
2 answers

ViewPager fragments recreation, are resumed but not visible

I have an issue with a ViewPager + FragmentPageAdapter. Scenario: I have one Activity with inside a Fragment A. Fragment A has a ViewPager with Fragment B1 e Fragment B2. B1 e B2 are the same class, but different data as…
Noodles
  • 3,263
  • 4
  • 19
  • 22
11
votes
2 answers

Android changing the background color for preference fragment

I have set a theme for my application, which includes a blue background... However, this made my preference fragment blue too. I want to keep the original "android:Theme.Holo.Light.DarkActionBar". How to do so? thanks!
MW_hk
  • 1,181
  • 4
  • 14
  • 39
11
votes
5 answers

NullPointerException in oncreateView() of Fragment

This code below shows part of activity where the fragment is added. FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.fragment_container, srkt_frag); …
11
votes
5 answers

Android - problems animating ActionBar icon from Fragments

I'm having some problems consistently animating a 'refresh' icon in the ActionBar of my app. I have a container FragmentActivity which swaps fragments in and out as the user navigates through the app (either from within the fragment itself or from a…
11
votes
5 answers

Fragments: Remove all fragments in a view

The scenario I am faced with, is in my application I have a single pane and dual pane style layout. Rather than individually handle every single navigation operation possible between the screens, for every different style of layout, I am using a…
btalb
  • 6,937
  • 11
  • 36
  • 44
11
votes
3 answers

How to write an Android multi-pane app with very deep navigation

TL;DR: How should multi-pane apps with deep navigation similar to the Spotify iPad app look and work on Android, and how to implement this? Long version: I'm working on an app, where the user sees lists of items and can then delve deeper into these…
Kevin Read
  • 2,173
  • 16
  • 23