<?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>fragments in android - Wikitechy</title>
	<atom:link href="https://www.wikitechy.com/interview-questions/tag/fragments-in-android/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.wikitechy.com/interview-questions/tag/fragments-in-android/</link>
	<description>Interview Questions</description>
	<lastBuildDate>Wed, 15 Sep 2021 06:12:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.wikitechy.com/interview-questions/wp-content/uploads/2025/10/cropped-wikitechy-icon-32x32.png</url>
	<title>fragments in android - Wikitechy</title>
	<link>https://www.wikitechy.com/interview-questions/tag/fragments-in-android/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Android Fragments</title>
		<link>https://www.wikitechy.com/interview-questions/android/android-fragments/</link>
					<comments>https://www.wikitechy.com/interview-questions/android/android-fragments/#respond</comments>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Sun, 11 Jul 2021 05:28:24 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Accenture interview questions and answers]]></category>
		<category><![CDATA[activity lifecycle in android]]></category>
		<category><![CDATA[add fragment to activity]]></category>
		<category><![CDATA[Agreeya Solutions interview questions and answers]]></category>
		<category><![CDATA[Allstate Solutions Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[android api]]></category>
		<category><![CDATA[android app development]]></category>
		<category><![CDATA[android app store]]></category>
		<category><![CDATA[android apps]]></category>
		<category><![CDATA[android apps download]]></category>
		<category><![CDATA[android developer]]></category>
		<category><![CDATA[android development]]></category>
		<category><![CDATA[android development tutorial]]></category>
		<category><![CDATA[android device]]></category>
		<category><![CDATA[android download]]></category>
		<category><![CDATA[android emulator]]></category>
		<category><![CDATA[android fragment]]></category>
		<category><![CDATA[android fragment example]]></category>
		<category><![CDATA[android fragment example source code]]></category>
		<category><![CDATA[android fragment lifecycle]]></category>
		<category><![CDATA[android fragment tutorial]]></category>
		<category><![CDATA[android lifecycle]]></category>
		<category><![CDATA[android multiple fragments in one activity example]]></category>
		<category><![CDATA[android phones]]></category>
		<category><![CDATA[android programming]]></category>
		<category><![CDATA[android sdk]]></category>
		<category><![CDATA[android service]]></category>
		<category><![CDATA[android software]]></category>
		<category><![CDATA[android studio fragment]]></category>
		<category><![CDATA[android studio tutorial]]></category>
		<category><![CDATA[android system]]></category>
		<category><![CDATA[android tutorial]]></category>
		<category><![CDATA[android update]]></category>
		<category><![CDATA[android versions]]></category>
		<category><![CDATA[android video]]></category>
		<category><![CDATA[apps for android]]></category>
		<category><![CDATA[best android phone]]></category>
		<category><![CDATA[Bharti Airtel interview questions and answers]]></category>
		<category><![CDATA[Bosch India Software interview questions and answers]]></category>
		<category><![CDATA[Capgemini interview questions and answers]]></category>
		<category><![CDATA[CASTING NETWORKS INDIA PVT LIMITED interview questions and answers]]></category>
		<category><![CDATA[CGI Group Inc interview questions and answers]]></category>
		<category><![CDATA[Chetu interview questions and answers]]></category>
		<category><![CDATA[Collabera Technologies interview questions and answers]]></category>
		<category><![CDATA[Dell International Services India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[developer android]]></category>
		<category><![CDATA[eclipse android]]></category>
		<category><![CDATA[Electronics Arts Inc interview questions and answers]]></category>
		<category><![CDATA[examples of fragments]]></category>
		<category><![CDATA[Flipkart interview questions and answers]]></category>
		<category><![CDATA[fragment android]]></category>
		<category><![CDATA[fragment android example]]></category>
		<category><![CDATA[fragment example in android studio]]></category>
		<category><![CDATA[fragment examples]]></category>
		<category><![CDATA[fragment lifecycle]]></category>
		<category><![CDATA[fragmentmanager]]></category>
		<category><![CDATA[fragments in android]]></category>
		<category><![CDATA[fragments in android example androidhive]]></category>
		<category><![CDATA[G-Cube Webwide Software Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[google android]]></category>
		<category><![CDATA[how to call fragment from activity in android example]]></category>
		<category><![CDATA[IBM interview questions and answers]]></category>
		<category><![CDATA[Indecomm Global Services interview questions and answers]]></category>
		<category><![CDATA[Infosys Technologies interview questions and answers]]></category>
		<category><![CDATA[Lava International Ltd interview questions and answers]]></category>
		<category><![CDATA[Maintec Technologies Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Mphasis interview questions and answers]]></category>
		<category><![CDATA[Oracle Corporation interview questions and answers]]></category>
		<category><![CDATA[PeopleStrong interview questions and answers]]></category>
		<category><![CDATA[Photon Interactive Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Prokarma Softech Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[SAP Labs India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Symphony Teleca interview questions and answers]]></category>
		<category><![CDATA[Tech Mahindra interview questions and answers]]></category>
		<category><![CDATA[UnitedHealth Group interview questions and answers]]></category>
		<category><![CDATA[what is a fragment]]></category>
		<category><![CDATA[what is android]]></category>
		<category><![CDATA[what is fragment]]></category>
		<category><![CDATA[Wipro interview questions and answers]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=106</guid>

					<description><![CDATA[Answer : A Fragment is a piece of an activity which enable more modular...]]></description>
										<content:encoded><![CDATA[<div class="ImageContent">
<div class="hddn" style="text-align: justify;"><img decoding="async" class="img-responsive center-block" src="https://cdn.wikitechy.com/tutorials/android/fragment-android.png" alt=" Android Fragments " /></div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="what-is-fragments" class="color-pink">What is Fragments?</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of sub-activity. A fragment has its own <a href="https://www.wikitechy.com/tutorials/android/layouts-in-android" target="_blank" rel="noopener">layout</a> and its own behavior with its own life cycle <a href="https://www.wikitechy.com/tutorials/android/android-call" target="_blank" rel="noopener">callbacks.</a></li>
</ul>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>You can add or remove fragments in an <a href="https://www.wikitechy.com/tutorials/android/android-start-activity" target="_blank" rel="noopener">activity</a> while the activity is running.</li>
<li>You can combine multiple fragments in a single activity to build a multi-plane UI.</li>
<li>Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an activity. Fragments represent multiple screen inside one activity.</li>
<li>Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity.</li>
<li>Each fragment has its own life cycle methods that is affected by <a href="https://www.wikitechy.com/tutorials/android/android-activity-lifecycle" target="_blank" rel="noopener">activity life cycle</a> because fragments are embedded in activity.</li>
<li>The Fragment Manager class is responsible to make interaction between fragment objects.</li>
</ul>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="android-fragment-lifecycle" class="color-purple">Android Fragment Lifecycle</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>The lifecycle of android fragment is like the activity lifecycle. There are 12 lifecycle methods for fragment.</li>
</ul>
</div>
</div>
<table class="table-bordered table-striped table table-responsive">
<tbody>
<tr>
<th class="text-center">No.</th>
<th class="text-center">Method</th>
<th class="text-center">Description</th>
</tr>
<tr>
<td class="text-leftalign">1.</td>
<td class="text-leftalign">onAttach(Activity)</td>
<td class="text-leftalign">it is called only once when it is attached with activity.</td>
</tr>
<tr>
<td class="text-leftalign">2.</td>
<td class="text-leftalign">onCreate(Bundle)</td>
<td class="text-leftalign">It is used to initialize the fragment.</td>
</tr>
<tr>
<td class="text-leftalign">3.</td>
<td class="text-leftalign">onCreateView(LayoutInflater, ViewGroup, Bundle)</td>
<td class="text-leftalign">creates and returns view hierarchy.</td>
</tr>
<tr>
<td class="text-leftalign">4.</td>
<td class="text-leftalign">onActivityCreated(Bundle)</td>
<td class="text-leftalign">It is invoked after the completion of onCreate() method.</td>
</tr>
<tr>
<td class="text-leftalign">5.</td>
<td class="text-leftalign">onViewStateRestored(Bundle)</td>
<td class="text-leftalign">It provides information to the fragment that all the saved state of fragment view hierarchy has been restored.</td>
</tr>
<tr>
<td class="text-leftalign">6.</td>
<td class="text-leftalign">onStart()</td>
<td class="text-leftalign">makes the fragment visible.</td>
</tr>
<tr>
<td class="text-leftalign">7.</td>
<td class="text-leftalign">onResume()</td>
<td class="text-leftalign">makes the fragment interactive.</td>
</tr>
<tr>
<td class="text-leftalign">8.</td>
<td class="text-leftalign">onPause()</td>
<td class="text-leftalign">is called when fragment is no longer interactive.</td>
</tr>
<tr>
<td class="text-leftalign">9.</td>
<td class="text-leftalign">onStop()</td>
<td class="text-leftalign">is called when fragment is no longer visible.</td>
</tr>
<tr>
<td class="text-leftalign">10.</td>
<td class="text-leftalign">onDestroyView()</td>
<td class="text-leftalign">allows the fragment to clean up resources.</td>
</tr>
<tr>
<td class="text-leftalign">11.</td>
<td class="text-leftalign">onDestroy()</td>
<td class="text-leftalign">allows the fragment to do final clean up of fragment state.</td>
</tr>
<tr>
<td class="text-leftalign">12.</td>
<td class="text-leftalign">onDetach()</td>
<td class="text-leftalign">It is called immediately prior to the fragment no longer being associated with its activity.</td>
</tr>
</tbody>
</table>
<div class="ImageContent" style="text-align: justify;">
<div class="hddn"><img decoding="async" class="img-responsive center-block" src="https://cdn.wikitechy.com/tutorials/android/android-fragment-life-cycle.png" alt="android fragment life cycle" /></div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="using-a-fragment" class="color-purple">Using a fragment</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>The general steps to use a Fragment:</li>
</ul>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li style="list-style-type: none;">
<ul>
<li>Create a subclass of Fragment.</li>
<li>Create a layout for the Fragment.</li>
<li>Add the Fragment to a host Activity, either statically or dynamically.</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="creating-a-fragment" class="color-purple">Creating a fragment</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>To create a Fragment in an app, extend the Fragment class, then override key lifecycle methods to insert your app logic, similar to the way of Activity class.</li>
<li>Instead of extending the base Fragment class, you can extend one of these other, more specific Fragment subclasses:
<ul>
<li><b>DialogFragment :</b> Displays a floating dialog, such as a date picker or time picker.</li>
<li><b>ListFragment :</b> Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter).</li>
<li><b>PreferenceFragment :</b> Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a &#8220;settings&#8221; Activity for your app.</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<h2 id="how-to-create-a-fragment-in-android-studio">How to create a Fragment in Android Studio</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>In Project: Android view, expand app &gt; java and select the folder containing the Java code for your app.</li>
<li>Choose File &gt; New &gt; Fragment &gt; Fragment (Blank).</li>
<li>Name the Fragment something like SimpleFragment, or use the supplied name (BlankFragment).</li>
<li>If your Fragment has a UI, check the Create layout XML option if it is not already checked. Other options include:</li>
</ul>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li style="list-style-type: none;">
<ul>
<li><b>Include fragment factory methods :</b> Include sample factory method code to initialize the Fragment arguments in a way that encapsulates and abstracts them. Select this option if the number of arguments would make a constructor too complex.</li>
<li><b>Include interface callbacks :</b> Select this option if you want to include sample code that defines an interface in the Fragment with callback methods that enable the Fragment to communicate with its host Activity.</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>Click Finish to create a Fragment.</li>
</ul>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="android-fragment-classes" class="color-purple">Android Fragment Classes</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>Fragments were added to the Android API in Honeycomb(API 11).
<ul>
<li>android.app.Fragment : The base class for all fragment definitions</li>
<li>android.app.FragmentManager : The class for interacting with fragment objects inside an activity</li>
<li>android.app.FragmentTransaction : The class for performing an atomic set of fragment operations</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>When using a compatibility package library provided by Google, the following classes are used for implementation.
<ul>
<li>android.support.v4.app.FragmentActivity : The base class for all activities using compatibility-based fragment (and loader) features</li>
<li>android.support.v4.app.Fragment</li>
<li>android.support.v4.app.FragmentManager</li>
<li>android.support.v4.app.FragmentTransaction</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="android-fragment-example" class="color-blue">Android Fragment Example</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>Let&#8217;s have a look at the simple example of android fragment.</li>
</ul>
</div>
</div>
<div class="subheading" style="text-align: justify;">
<h2 id="activity_main-xml">activity_main.xml</h2>
</div>
<div class="CodeContent" style="text-align: justify;">
<div class="hddn">
<figure class="highlight"><div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;  <br/>    android:layout_width=&quot;fill_parent&quot;  <br/>    android:layout_height=&quot;fill_parent&quot; &gt;  <br/>  <br/>    &lt;fragment  <br/>        android:id=&quot;@+id/fragment2&quot;  <br/>        android:name=&quot;com.example.fragmentexample.Fragment2&quot;  <br/>        android:layout_width=&quot;0px&quot;  <br/>        android:layout_height=&quot;match_parent&quot;   <br/>        android:layout_weight=&quot;1&quot;  <br/>        /&gt;  <br/>  <br/>    &lt;fragment  <br/>        android:id=&quot;@+id/fragment1&quot;  <br/>        android:name=&quot;com.example.fragmentexample.Fragment1&quot;  <br/>        android:layout_width=&quot;0px&quot;  <br/>        android:layout_height=&quot;match_parent&quot;  <br/>        android:layout_weight=&quot;1&quot;  <br/>         /&gt;  <br/>  <br/>&lt;/LinearLayout&gt;  </code></pre> </div></figure>
</div>
</div>
<div class="subheading" style="text-align: justify;">
<h2 id="file-fragment1-xml">File: fragment1.xml</h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;  <br/>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;  <br/>    android:layout_width=&quot;match_parent&quot;  <br/>    android:layout_height=&quot;match_parent&quot;  <br/>    android:orientation=&quot;vertical&quot;  <br/>    android:background=&quot;#00ff00&quot;  <br/>     &gt;  <br/>  <br/>    &lt;TextView  <br/>        android:id=&quot;@+id/textView1&quot;  <br/>        android:layout_width=&quot;wrap_content&quot;  <br/>        android:layout_height=&quot;wrap_content&quot;  <br/>        android:text=&quot;fragment frist&quot;  <br/>        android:textAppearance=&quot;?android:attr/textAppearanceLarge&quot; /&gt;  <br/>  <br/>&lt;/LinearLayout&gt;</code></pre> </div>
</div>
<div class="subheading" style="text-align: justify;">
<h2 id="file-fragment2-xml">File: fragment2.xml</h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;  <br/>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;  <br/>    android:layout_width=&quot;match_parent&quot;  <br/>    android:layout_height=&quot;match_parent&quot;  <br/>    android:orientation=&quot;vertical&quot;  <br/>    android:background=&quot;#0000ff&quot;  <br/>     &gt;  <br/>  <br/>    &lt;TextView  <br/>        android:id=&quot;@+id/textView1&quot;  <br/>        android:layout_width=&quot;wrap_content&quot;  <br/>        android:layout_height=&quot;wrap_content&quot;  <br/>        android:text=&quot;Second Fragment&quot;  <br/>        android:textAppearance=&quot;?android:attr/textAppearanceLarge&quot; /&gt;  <br/>  <br/>&lt;/LinearLayout&gt;</code></pre> </div>
</div>
<div class="subheading" style="text-align: justify;">
<h2 id="mainactivity-class">MainActivity class</h2>
</div>
<div class="subheading" style="text-align: justify;">
<h2 id="file-mainactivity-java">File: MainActivity.java</h2>
</div>
<div class="CodeContent" style="text-align: justify;">
<div class="hddn">
<figure class="highlight"><div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">package com.example.fragmentexample;  <br/>  <br/>import android.os.Bundle;  <br/>import android.app.Activity;  <br/>import android.view.Menu;  <br/>public class MainActivity extends Activity {  <br/>  <br/>    @Override  <br/>    protected void onCreate(Bundle savedInstanceState) {  <br/>        super.onCreate(savedInstanceState);  <br/>        setContentView(R.layout.activity_main);  <br/>    }  <br/>}</code></pre> </div></figure>
</div>
</div>
<div class="subheading" style="text-align: justify;">
<h2 id="file-fragment1-java">File: Fragment1.java</h2>
</div>
<div class="CodeContent" style="text-align: justify;">
<div class="hddn">
<figure class="highlight"><div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">ackage com.example.fragmentexample;  <br/>  <br/>import android.app.Fragment;  <br/>import android.os.Bundle;  <br/>import android.view.LayoutInflater;  <br/>import android.view.View;  <br/>import android.view.ViewGroup;  <br/>  <br/>public class Fragment1 extends Fragment {  <br/>    @Override  <br/>    public View onCreateView(LayoutInflater inflater, ViewGroup container,  <br/>            Bundle savedInstanceState) {  <br/>        // TODO Auto-generated method stub  <br/>        return inflater.inflate(R.layout.fragment1,container, false);  <br/>    }  <br/>  <br/>} </code></pre> </div></figure>
</div>
</div>
<div class="ImageContent" style="text-align: justify;">
<div class="hddn"><img decoding="async" class="img-responsive center-block aligncenter" src="https://cdn.wikitechy.com/tutorials/android/android-activity-cycle.png" alt="android activity and fragment life cycle" /></div>
</div>
<div class="subheading">
<h2 id="file-fragment2-java" style="text-align: justify;">File: Fragment2.java</h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">package com.example.fragmentexample;  <br/>  <br/>import android.app.Fragment;  <br/>import android.os.Bundle;  <br/>import android.view.LayoutInflater;  <br/>import android.view.View;  <br/>import android.view.ViewGroup;  <br/>  <br/>public class Fragment2 extends Fragment {  <br/>      <br/>    public View onCreateView(LayoutInflater inflater, ViewGroup container,  <br/>            Bundle savedInstanceState) {  <br/>        // TODO Auto-generated method stub  <br/>        return inflater.inflate(R.layout.fragment2,container, false);  <br/>    }  <br/>  <br/>} </code></pre> </div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="output" class="color-green">Output</h2>
</div>
</div>
<div class="ImageContent">
<div class="hddn"><img decoding="async" class="img-responsive center-block aligncenter" src="https://cdn.wikitechy.com/tutorials/android/android-fragment.png" alt=" Android Fragments " /></div>
</div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/android/android-fragments/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
