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

Different menu for different tabs in tab+swipe application for android project

I am a beginner to android applications and java, basically I am a PHP developer. I've a project for a tab+swipe application, Reseller.java package com.idevoc.onsitereseller; import java.util.ArrayList; import android.app.ActionBar; import…
Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
11
votes
3 answers

Why should I use fragment in Android?

For very long time, I think what is the reason of using fragment in Android if I just develop the application for Android Phone only but not 10.1. Is it necessary to use fragment? Also, what is the usage of fragment, I found that it may use for…
jjLin
  • 3,281
  • 8
  • 32
  • 55
11
votes
1 answer

What's the difference between using add().addToBackStack(), add().detach() and replace().addToBackStack() in a FragmentTransaction?

In FragmentTransaction item on Android docs, it is described that the method replace() is the same as calling the method remove() for all fragments added in the currently view and then called the method add(). In this case, to recover the previous…
mari
  • 864
  • 3
  • 12
  • 32
11
votes
2 answers

Android Actionbar Sherlock with Tabs

I am trying to implement ActionBar Sherlock with Tabs below that as shown in the above wire-frame. Should i use TabActivity ? - since i saw that it is deprecated. Which is the best way to achieve the same.
11
votes
3 answers

VideoView is not displayed on a Fragment

I have a problem in running a video in Samsung S3(Android 4.1.1), the issue seems to be because the videoview is on a fragment because if I put it on and activity, it works. Also I found out that if I turn on the GPU hardware acceleration on, the…
Cata
  • 11,133
  • 11
  • 65
  • 86
11
votes
2 answers

How to call getBaseContext() on Fragment

I have this method which can be called from normal Activity: getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics()); The problem is I cannot call getBaseContext() in Fragment, how can…
hectichavana
  • 1,436
  • 13
  • 41
  • 71
11
votes
3 answers

ShareActionProvider refresh text

I have an EditText which is initially filled with text from the local db. When the user leaves the screen (onPause), the updated text is stored in the local db. I also have a ShareActionProvider (using ActionBarSherlock). When the user uses the…
11
votes
1 answer

When a fragment has lost focus and then regains focus, why its onResume() method is never called in the period?

Fragment A has got focus. When Fragment B is created, B gains the focus now. This causes A to lose focus, but it is still visible. Now if B is destroyed, A gains the focus again. As A regains focus, does its onResume() method get called? If not,…
Europa
  • 147
  • 1
  • 1
  • 8
11
votes
4 answers

Add buttons to PreferenceFragment

I want to add a couple buttons to the bottom of my preferences screen for setting defaults and restoring defaults. This answer doesn't cover how to do this using PreferenceFragment. What is the recommended way to accomplish this. Activity that loads…
Namaste
  • 265
  • 1
  • 4
  • 11
11
votes
3 answers

commit fragment from onLoadFinished within activity

I have an activity which loads a data list from the server using loader callbacks. I have to list out the data into a fragment which extends SherlockListFragment i tried to commit the fragment using Fragment newFragment =…
11
votes
2 answers

Complex Android UI design guidance needed (fragments)

I am developing an applications that is aimed at Tablets and Google TVs. It will be like many standard Google TV applications with a LeftNavBar and a top Search bar that is common to all application screens. It will look something like the following…
Aamir
  • 1,747
  • 5
  • 26
  • 50
11
votes
3 answers

Switching between Fragments in a single Activity

I want to create an Activity which shows a sort of menu a user can go through. By clicking an item, a new screen is shown, allowing the user more options (wizard-like). I wanted to implement this using Fragments, but it's not working for me. Right…
nhaarman
  • 98,571
  • 55
  • 246
  • 278
11
votes
2 answers

Adding buttons programmatically to a fragment

I'm trying to programmatically add image buttons to the fragment that is a part of a viewpager. I tried different codes, but no button shows up even though Eclipse returns no error. I found a similar question here but the answers didn't help me…
Jason O.
  • 3,168
  • 6
  • 33
  • 72
11
votes
3 answers

Can I create Fragment ListActivity with compatibility package v4?

I've been trying to complete tutorial "Working with fragments" which was designed for android 3.0+ To acomplish that I'm using compatibility package v4 (since my API is 7). However I'm stuck on the lists. I couldn't find straight answer. I'm getting…
Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157
11
votes
10 answers

ListFragment does not accept my layout

According to this link: ListFragment android developers I want to set my custom layout for list, but it makes exceptions. Here is the code: public class ListFragmentActivity extends FragmentActivity { @Override public void onCreate(Bundle…
mohamed zakria
  • 141
  • 1
  • 1
  • 6