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:

44378 questions
1111
votes
36 answers

findViewById in Fragment

I am trying to create an ImageView in a Fragment which will refer to the ImageView element which I have created in the XML for the Fragment. However, the findViewById method only works if I extend an Activity class. Is there anyway of which I can…
simplified.
  • 11,237
  • 5
  • 14
  • 8
936
votes
39 answers

onActivityResult is not being called in Fragment

The activity hosting this fragment has its onActivityResult called when the camera activity returns. My fragment starts an activity for a result with the intent sent for the camera to take a picture. The picture application loads fine, takes a…
Spidy
  • 38,131
  • 15
  • 59
  • 77
832
votes
16 answers

Dilemma: when to use Fragments vs Activities:

I know that Activities are designed to represent a single screen of my application, while Fragments are designed to be reusable UI layouts with logic embedded inside of them. Until not long ago, I developed an application as it said that they should…
Emil Adz
  • 38,699
  • 35
  • 127
  • 177
789
votes
27 answers

How to determine when Fragment becomes visible in ViewPager

Problem: Fragment onResume() in ViewPager is fired before the fragment becomes actually visible. For example, I have 2 fragments with ViewPager and FragmentPagerAdapter. The second fragment is only available for authorized users and I need to ask…
4ntoine
  • 17,607
  • 16
  • 70
  • 175
741
votes
14 answers

Best practice for instantiating a new Android Fragment

I have seen two general practices to instantiate a new Fragment in an application: Fragment newFragment = new MyFragment(); and Fragment newFragment = MyFragment.newInstance(); The second option makes use of a static method newInstance() and…
Graham Smith
  • 24,739
  • 10
  • 43
  • 68
704
votes
31 answers

Using context in a fragment

How can I get the context in a fragment? I need to use my database whose constructor takes in the context, but getApplicationContext() and FragmentClass.this don't work so what can I do? Database constructor public Database(Context ctx) { …
tyczj
  • 66,691
  • 50
  • 172
  • 271
540
votes
11 answers

Why fragments, and when to use fragments instead of activities?

In Android API 11+, Google has released a new class called Fragment. In the videos, Google suggests that whenever possible (link1, link2), we should use fragments instead of activities, but they didn't explain exactly why. What's the purpose of…
533
votes
51 answers

How to implement onBackPressed() in Fragments?

Is there a way in which we can implement onBackPressed() in Android Fragment similar to the way in which we implement in Android Activity? As the Fragment lifecycle do not have onBackPressed(). Is there any other alternative method to over ride…
Android_programmer_camera
  • 11,775
  • 20
  • 64
  • 80
524
votes
33 answers

IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager

I'm getting user reports from my app in the market, delivering the following exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at…
514
votes
6 answers

How to correctly save instance state of Fragments in back stack?

I have found many instances of a similar question on SO but no answer unfortunately meets my requirements. I have different layouts for portrait and landscape and I am using back stack, which both prevents me from using setRetainState() and tricks…
The Vee
  • 10,647
  • 5
  • 22
  • 50
498
votes
11 answers

ViewPager and fragments — what's the right way to store fragment's state?

Fragments seem to be very nice for separation of UI logic into some modules. But along with ViewPager its lifecycle is still misty to me. So Guru thoughts are badly needed! Edit See dumb solution below ;-) Scope Main activity has a ViewPager with…
485
votes
52 answers

How do I get the currently displayed fragment?

I am playing with fragments in Android. I know I can change a fragment by using the following code: FragmentManager fragMgr = getSupportFragmentManager(); FragmentTransaction fragTrans = fragMgr.beginTransaction(); MyFragment myFragment = new…
Leem.fin
  • 35,699
  • 70
  • 166
  • 297
450
votes
19 answers

How to handle button clicks using the XML onClick within Fragments

Pre-Honeycomb (Android 3), each Activity was registered to handle button clicks via the onClick tag in a Layout's XML: android:onClick="myClickMethod" Within that method you can use view.getId() and a switch statement to do the button logic. With…
smith324
  • 12,886
  • 9
  • 34
  • 57
429
votes
20 answers

How to add Options Menu to Fragment in Android

I am trying to add an item to the options menu from a group of fragments. I have created a new MenuFragment class and extended this for the fragments I wish to include the menu item in. Here is the code: Java: public class MenuFragment extends…
misterbassman
  • 4,309
  • 2
  • 12
  • 4
410
votes
13 answers

Fragment MyFragment not attached to Activity

I've created a small test app which represents my problem. I'm using ActionBarSherlock to implement tabs with (Sherlock)Fragments. My code: TestActivity.java public class TestActivity extends SherlockFragmentActivity { private ActionBar…
nhaarman
  • 90,392
  • 51
  • 233
  • 265
1
2 3
99 100