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
435
votes
14 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
  • 98,571
  • 55
  • 246
  • 278
404
votes
9 answers

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter? About FragmentPagerAdapter Google's guide says: This version of the pager is best for use when there are a handful of typically more static fragments to be paged…
379
votes
18 answers

Call an activity method from a fragment

Trying to call a method in my activity from a fragment. I want the fragment to give the method data and to get the data when the method return. I want to achieve similar to call on a static method, but without the use of static because it create…
Joakim Ericsson
  • 4,616
  • 3
  • 21
  • 29
374
votes
25 answers

Android Fragment handle back button press

I have some fragments in my activity [1], [2], [3], [4], [5], [6] And on Back Button Press I must to return from [2] to [1] if current active fragment is [2], or do nothing otherwise. What is the best practise to do that? EDIT: Application must not…
skayred
  • 10,603
  • 10
  • 52
  • 94
373
votes
3 answers

Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

What are the differences between onCreate(), onCreateView(), and onActivityCreated() in fragments and what would they each be used for?
366
votes
5 answers

Understanding Fragment's setRetainInstance(boolean)

Starting with the documentation: public void setRetainInstance (boolean retain) Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back…
User
  • 31,811
  • 40
  • 131
  • 232
361
votes
24 answers

Send data from activity to fragment in Android

I have two classes. First is activity, second is a fragment where I have some EditText. In activity I have a subclass with async-task and in method doInBackground I get some result, which I save to variable. How can I send this variable from…
user1302569
  • 7,131
  • 13
  • 46
  • 66
360
votes
9 answers

Difference between add(), replace(), and addToBackStack()

What is the main difference between calling these methods: fragmentTransaction.addToBackStack(name); fragmentTransaction.replace(containerViewId, fragment, tag); fragmentTransaction.add(containerViewId, fragment, tag); What does it mean to replace…
AndroidDev
  • 15,993
  • 29
  • 85
  • 119
352
votes
6 answers

Wrong requestCode in onActivityResult

I'm starting a new Activity from my Fragment with startActivityForResult(intent, 1); and want to handle the result in the Fragment's parent Activity: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { …
Dimanoid
  • 6,999
  • 4
  • 40
  • 55
351
votes
40 answers

Android Fragment no view found for ID?

I have a fragment I am trying to add into a view. FragmentManager fragMgr=getSupportFragmentManager(); feed_parser_activity content = (feed_parser_activity)fragMgr …
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
344
votes
10 answers

Android Fragment onAttach() deprecated

I have updated my app to use the latest support library (version 23.0.0), I've found out that they deprecated the onAttach() function of the Fragment class. Instead of: onAttach (Activity activity) It's now: onAttach (Context context) As my app…
TareK Khoury
  • 12,721
  • 16
  • 55
  • 78
344
votes
23 answers

Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

I have an application with three tabs. Each tab has its own layout .xml file. The main.xml has its own map fragment. It's the one that shows up when the application first launches. Everything works fine except for when I change between tabs. If I…
hermann
  • 6,237
  • 11
  • 46
  • 66
336
votes
21 answers

Update ViewPager dynamically?

I can't update the content in ViewPager. What is the correct usage of methods instantiateItem() and getItem() in FragmentPagerAdapter class? I was using only getItem() to instantiate and return my fragments: @Override public Fragment getItem(int…
User
  • 31,811
  • 40
  • 131
  • 232
319
votes
7 answers

Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

I'm coming from iOS where it's easy and you simply use a UIViewController. However, in Android things seem much more complicated, with certain UIComponents for specific API Levels. I'm reading BigNerdRanch for Android (the book is roughly 2 years…
309
votes
12 answers

Fragment over another fragment issue

When I'm showing one fragment (which is full screen with #77000000 background) over another fragment (let's call it main), my main fragment still reacts to clicks (we can click a button even if we don't see it). Question: how to prevent clicks on…
Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146