<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fragment &#8211; Wiebe Elsinga</title>
	<atom:link href="http://wiebe-elsinga.com/blog/tag/fragment/feed/" rel="self" type="application/rss+xml" />
	<link>http://wiebe-elsinga.com/blog</link>
	<description>Blog</description>
	<lastBuildDate>Fri, 01 Feb 2013 15:15:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.1</generator>
	<item>
		<title>AndroidDev (Pro) tip: Fragment meets container</title>
		<link>http://wiebe-elsinga.com/blog/androiddev-pro-tip-fragment-meets-container/</link>
					<comments>http://wiebe-elsinga.com/blog/androiddev-pro-tip-fragment-meets-container/#respond</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Fri, 01 Feb 2013 08:04:58 +0000</pubDate>
				<category><![CDATA[AndroidDev Tips]]></category>
		<category><![CDATA[AndroidDev]]></category>
		<category><![CDATA[Container]]></category>
		<category><![CDATA[Fragment]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=1415</guid>

					<description><![CDATA[Today a tip from my colleague Coen Houtman, a tip about Fragments and the container to put them in. What all of you Fragment users know, is that you must have a container (i.e. ViewGroup) to put your fragments in. Which ViewGroup is the best to use? Normally, with any other layout I want to create, I start with a LinearLayout. It&#8217;s simple, easy and lightweight; something that you will experience in more complex layouts. If you use LinearLayout as the container for your Fragments, you will probably run into some issues. In the image below, the content doesn&#8217;t even take up half the screen. Even though the width was set to match it&#8217;s parent. The solution to the problem above: use FrameLayout! I&#8217;m not claiming that FrameLayout is always the way to go. In all our projects though, we use FrameLayout as the container for our Fragments. At the moment I don&#8217;t have the answer of why FrameLayout works better than LinearLayout. But when I do, you&#8217;ll read it here!]]></description>
										<content:encoded><![CDATA[<p>Today a tip from my colleague Coen Houtman, a tip about Fragments and the container to put them in. What all of you Fragment users know, is that you must have a container (i.e. ViewGroup) to put your fragments in.</p>
<h4>Which ViewGroup is the best to use?</h4>
<p>Normally, with any other layout I want to create, I start with a LinearLayout. It&#8217;s simple, easy and lightweight; something that you will experience in more complex layouts. If you use LinearLayout as the container for your Fragments, you will probably run into some issues. In the image below, the content doesn&#8217;t even take up half the screen. Even though the width was set to match it&#8217;s parent.</p>
<p style="text-align: justify;"><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146.png" rel="lightbox[1415]" title="AndroidDev (Pro) tip: Fragment meets container"><img loading="lazy" class="aligncenter size-full wp-image-1417" alt="device-2012-12-14-111146" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146.png" width="187" height="300" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146.png 800w, http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146-187x300.png 187w, http://wiebe-elsinga.com/blog/wp-content/uploads/2013/02/device-2012-12-14-111146-640x1024.png 640w" sizes="(max-width: 187px) 100vw, 187px" /></a></p>
<p>The solution to the problem above: use FrameLayout! I&#8217;m not claiming that FrameLayout is always the way to go. In all our projects though, we use FrameLayout as the container for our Fragments. At the moment I don&#8217;t have the answer of why FrameLayout works better than LinearLayout. But when I do, you&#8217;ll read it here!</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/androiddev-pro-tip-fragment-meets-container/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How-To: Android Fragments</title>
		<link>http://wiebe-elsinga.com/blog/android-fragment/</link>
					<comments>http://wiebe-elsinga.com/blog/android-fragment/#comments</comments>
		
		<dc:creator><![CDATA[W.Elsinga]]></dc:creator>
		<pubDate>Tue, 22 Mar 2011 02:12:10 +0000</pubDate>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Fragment]]></category>
		<category><![CDATA[Honeycomb]]></category>
		<guid isPermaLink="false">http://wiebe-elsinga.com/blog/?p=578</guid>

					<description><![CDATA[Typically, an Android user interface is composed of views within layouts, such as a ListView within a LinearLayout. A hierarchy of View objects gets loaded or created when an Activity is started. On small screens, this is fine. But when you spread a UI out over the surface of a tablet&#8217;s screen, it calls for a different style of interaction. Some parts of the screen should remain constant over longer durations than others While developers can implement interactions where parts of the screen change by showing and hiding views, Android&#8217;s developers decided they needed more than just convention to encourage better large screen UIs and consistent implementation of the feel of the UI. Now, in order to facilitate this new kind of interaction, as part of the Android 3.0 SDK, we get a class called Fragment. A Fragment object is something between a View and and Activity: It can be part of a layout, but it isn&#8217;t a subclass of View. It implements the ComponentCallbacks interface, and it has a lifecycle, but that lifecycle is dependent on the Activity the Fragment object belongs to. Let&#8217;s see what it can do in a tablet-sized user interface. Here are the contents of the main.xml file that describes a screen layout for our application. In a list on the left of the screen some numbers will appear. Selecting a number from that list will create that many Android icons, displayed in a Fragment instance on the right. &#60;!--?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34;?--&#62; xmlns:android=&#34;http://schemas.android.com/apk/res/android&#34; android:orientation=&#34;horizontal&#34; android:layout_width=&#34;match_parent&#34; android:layout_height=&#34;match_parent&#34; android:id=&#34;@+id/frags&#34;&#62; android:id=&#34;@+id/number_list&#34; android:layout_width=&#34;250dip&#34; android:layout_height=&#34;match_parent&#34; /&#62; android:id=&#34;@+id/the_frag&#34; android:layout_width=&#34;match_parent&#34; android:layout_height=&#34;match_parent&#34; /&#62; The code for the Fragment subclass referred to in the layout in main.xml is below. package com.my; import android.app.Fragment; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; public class FragmentExample extends Fragment &#123; private int nAndroids; public FragmentExample&#40;&#41; &#123; &#125; /** * Constructor for being created explicitly */ public FragmentExample&#40;int nAndroids&#41; &#123; this.nAndroids = nAndroids; &#125; /** * If we are being created with saved state, restore our state */ @Override public void onCreate&#40;Bundle saved&#41; &#123; super.onCreate&#40;saved&#41;; if &#40;null != saved&#41; &#123; nAndroids = saved.getInt&#40;&#34;nAndroids&#34;&#41;; &#125; &#125; /** * Save the number of Androids to be displayed */ @Override public void onSaveInstanceState&#40;Bundle toSave&#41; &#123; toSave.putInt&#40;&#34;nAndroids&#34;, nAndroids&#41;; &#125; /** * Make a grid and fill it with n Androids */ @Override public View onCreateView&#40;LayoutInflater inflater, ViewGroup container, Bundle saved&#41; &#123; int n; Context c = getActivity&#40;&#41;.getApplicationContext&#40;&#41;; LinearLayout l = new LinearLayout&#40;c&#41;; for &#40;n = 0; n &#60; nAndroids; n++&#41; &#123; ImageView i = new ImageView&#40;c&#41;; i.setImageResource&#40;R.drawable.android&#41;; l.addView&#40;i&#41;; &#125; return l; &#125; &#125; package com.my; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; public class FragmentExampleActivity extends Activity implements OnItemClickListener &#123; /** Called when the activity is first created. */ @Override public void onCreate&#40;Bundle savedInstanceState&#41; &#123; super.onCreate&#40;savedInstanceState&#41;; setContentView&#40;R.layout.main&#41;; ListView l = &#40;ListView&#41; findViewById&#40;R.id.number_list&#41;; ArrayAdapter numbers = new ArrayAdapter&#40; getApplicationContext&#40;&#41;, android.R.layout.simple_list_item_1, new String&#91;&#93; &#123; &#34;one&#34;, &#34;two&#34;, &#34;three&#34;, &#34;four&#34;, &#34;five&#34;, &#34;six&#34; &#125;&#41;; l.setAdapter&#40;numbers&#41;; l.setOnItemClickListener&#40;this&#41;; &#125; /** * Add a Fragment to our stack with n Androids in it */ private void stackAFragment&#40;int nAndroids&#41; &#123; Fragment f = new FragmentExample&#40;nAndroids&#41;; FragmentTransaction ft = getFragmentManager&#40;&#41;.beginTransaction&#40;&#41;; ft.replace&#40;R.id.the_frag, f&#41;; ft.setTransition&#40;FragmentTransaction.TRANSIT_FRAGMENT_OPEN&#41;; ft.addToBackStack&#40;null&#41;; ft.commit&#40;&#41;; &#125; /** * Called when a number gets clicked */ public void onItemClick&#40;AdapterView&#60;!--?--&#62; parent, View view, int position, long id&#41; &#123; stackAFragment&#40;position + 1&#41;; &#125; &#125; This is what you see when you when you run the program: Initially, you see a list of numbers, and a blank area to the right. Touch a number, and a new Fragment is created, with the specified number of Android icons. If you keep touching numbers in the list on the left, the corresponding number of Android icons appears in the Fragment instance on the right. Now try the back button. Instead of returning to an instance of the Activity that was on the screen before this application launched, you see the previous Fragment instance on the right. That is, the Fragment objects have been integrated into the task&#8217;s back stack. Fragments are a new feature of Android, and this brief example only scratches the surface of how Fragment will change the way Android UIs are implemented. You can see from this example that using Fragments is efficient and expressive. You can read more about Fragment, and how it can be used alongside starting Activity objects to drill into detail views in an Android UI in an article by Dianne Hackborn, here. Download source code FragmentExample [19kB]]]></description>
										<content:encoded><![CDATA[<p>Typically, an Android user interface is composed of views within layouts, such as a ListView within a LinearLayout. A hierarchy of View objects gets loaded or created when an Activity is started. On small screens, this is fine.<br />
But when you spread a UI out over the surface of a tablet&#8217;s screen, it calls for a different style of interaction. Some parts of the screen should remain constant over longer durations than others</p>
<p>While developers can implement interactions where parts of the screen change by showing and hiding views, Android&#8217;s developers decided they needed more than just convention to encourage better large screen UIs and consistent implementation of the feel of the UI. Now, in order to facilitate this new kind of interaction, as part of the Android 3.0 SDK, we get a class called Fragment.<br />
<span id="more-578"></span><br />
A Fragment object is something between a View and and Activity: It can be part of a layout, but it isn&#8217;t a subclass of View.<br />
It implements the ComponentCallbacks interface, and it has a lifecycle, but that lifecycle is dependent on the Activity the Fragment object belongs to.<br />
Let&#8217;s see what it can do in a tablet-sized user interface.</p>
<p>Here are the contents of the <em>main.xml</em> file that describes a screen layout for our application. In a list on the left of the screen some numbers will appear. Selecting a number from that list will create that many Android icons, displayed in a Fragment instance on the right.</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?--&gt;</span><br />
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br />
android:orientation=&quot;horizontal&quot;<br />
android:layout_width=&quot;match_parent&quot;<br />
android:layout_height=&quot;match_parent&quot;<br />
android:id=&quot;@+id/frags&quot;&gt;<br />
<br />
android:id=&quot;@+id/number_list&quot;<br />
android:layout_width=&quot;250dip&quot;<br />
android:layout_height=&quot;match_parent&quot; /&gt;<br />
<br />
android:id=&quot;@+id/the_frag&quot;<br />
android:layout_width=&quot;match_parent&quot;<br />
android:layout_height=&quot;match_parent&quot; /&gt;</div></div>
<p>The code for the Fragment subclass referred to in the layout in <em>main.xml</em> is below.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.my</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Fragment</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.LayoutInflater</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.ViewGroup</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ImageView</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.LinearLayout</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FragmentExample <span style="color: #000000; font-weight: bold;">extends</span> Fragment <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> nAndroids<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> FragmentExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Constructor for being created explicitly<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> FragmentExample<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> nAndroids<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nAndroids</span> <span style="color: #339933;">=</span> nAndroids<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* If we are being created with saved state, restore our state<br />
*/</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle saved<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>saved<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">!=</span> saved<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
nAndroids <span style="color: #339933;">=</span> saved.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nAndroids&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Save the number of Androids to be displayed<br />
*/</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSaveInstanceState<span style="color: #009900;">&#40;</span>Bundle toSave<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
toSave.<span style="color: #006633;">putInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;nAndroids&quot;</span>, nAndroids<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Make a grid and fill it with n Androids<br />
*/</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+view"><span style="color: #003399;">View</span></a> onCreateView<span style="color: #009900;">&#40;</span>LayoutInflater inflater, ViewGroup container, Bundle saved<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #339933;">;</span><br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+context"><span style="color: #003399;">Context</span></a> c <span style="color: #339933;">=</span> getActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getApplicationContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
LinearLayout l <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinearLayout<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> n <span style="color: #339933;">&lt;</span> nAndroids<span style="color: #339933;">;</span> n<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
ImageView i <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ImageView<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
i.<span style="color: #006633;">setImageResource</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">android</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
l.<span style="color: #006633;">addView</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">return</span> l<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:100%;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.my</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Fragment</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.FragmentTransaction</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.view.View</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.AdapterView</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ArrayAdapter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ListView</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.AdapterView.OnItemClickListener</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FragmentExampleActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #000000; font-weight: bold;">implements</span><br />
OnItemClickListener <span style="color: #009900;">&#123;</span><br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/** Called when the activity is first created. */</span><br />
@Override<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+listview"><span style="color: #003399;">ListView</span></a> l <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+listview"><span style="color: #003399;">ListView</span></a><span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">number_list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ArrayAdapter numbers <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayAdapter<span style="color: #009900;">&#40;</span><br />
getApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, android.<span style="color: #006633;">R</span>.<span style="color: #006633;">layout</span>.<span style="color: #006633;">simple_list_item_1</span>,<br />
<span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+string"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;one&quot;</span>, <span style="color: #0000ff;">&quot;two&quot;</span>, <span style="color: #0000ff;">&quot;three&quot;</span>, <span style="color: #0000ff;">&quot;four&quot;</span>, <span style="color: #0000ff;">&quot;five&quot;</span>, <span style="color: #0000ff;">&quot;six&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
l.<span style="color: #006633;">setAdapter</span><span style="color: #009900;">&#40;</span>numbers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
l.<span style="color: #006633;">setOnItemClickListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Add a Fragment to our stack with n Androids in it<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> stackAFragment<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> nAndroids<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
Fragment f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FragmentExample<span style="color: #009900;">&#40;</span>nAndroids<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
FragmentTransaction ft <span style="color: #339933;">=</span> getFragmentManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ft.<span style="color: #006633;">replace</span><span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">the_frag</span>, f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ft.<span style="color: #006633;">setTransition</span><span style="color: #009900;">&#40;</span>FragmentTransaction.<span style="color: #006633;">TRANSIT_FRAGMENT_OPEN</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ft.<span style="color: #006633;">addToBackStack</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
ft.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
* Called when a number gets clicked<br />
*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onItemClick<span style="color: #009900;">&#40;</span>AdapterView<span style="color: #339933;">&lt;!--?--&gt;</span> parent, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+view"><span style="color: #003399;">View</span></a> view, <span style="color: #000066; font-weight: bold;">int</span> position, <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
stackAFragment<span style="color: #009900;">&#40;</span>position <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>This is what you see when you when you run the program: Initially, you see a list of numbers, and a blank area to the right.</p>
<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag1.gif" rel="lightbox[578]" title="frag1"><img loading="lazy" class="aligncenter size-medium wp-image-581" title="frag1" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag1-300x191.gif" alt="" width="300" height="191" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag1-300x191.gif 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag1.gif 669w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Touch a number, and a new Fragment is created, with the specified number of Android icons. If you keep touching numbers in the list on the left, the corresponding number of Android icons appears in the Fragment instance on the right. Now try the back button. Instead of returning to an instance of the Activity that was on the screen before this application launched, you see the previous Fragment instance on the right. That is, the Fragment objects have been integrated into the task&#8217;s back stack.</p>
<p><a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag2.gif" rel="lightbox[578]" title="frag2"><img loading="lazy" class="aligncenter size-medium wp-image-582" title="frag2" src="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag2-300x192.gif" alt="" width="300" height="192" srcset="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag2-300x193.gif 300w, http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/frag2.gif 666w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>Fragments are a new feature of Android, and this brief example only scratches the surface of how Fragment will change the way Android UIs are implemented. You can see from this example that using Fragments is efficient and expressive.<br />
You can read more about Fragment, and how it can be used alongside starting Activity objects to drill into detail views in an Android UI in an article by Dianne Hackborn, <a href="http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html">here</a>.</p>
<p>Download source code <a href="http://wiebe-elsinga.com/blog/wp-content/uploads/2011/03/FragmentExample.zip">FragmentExample</a> [19kB]</p>
]]></content:encoded>
					
					<wfw:commentRss>http://wiebe-elsinga.com/blog/android-fragment/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
