0% found this document useful (0 votes)
10 views14 pages

Lab5 PDM

The document provides an overview of using Fragments in Android Studio, explaining their lifecycle, communication with Activities, and layout design. It details how to create and manage Fragments, including sending data between Activities and Fragments using Bundles. Additionally, it includes code examples for implementing Fragments within an Android application.

Uploaded by

Alexandru Takacs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Lab5 PDM

The document provides an overview of using Fragments in Android Studio, explaining their lifecycle, communication with Activities, and layout design. It details how to create and manage Fragments, including sending data between Activities and Fragments using Bundles. Additionally, it includes code examples for implementing Fragments within an Android application.

Uploaded by

Alexandru Takacs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PMD – Lab5  Android Studio

Android
Studio

Fragments
Lab5, Android Studio Fragments (MyFragments project)
Fragments are reusable component that encapsulates functionality.

Fragment has its own life cycle but it depends on its Activity.

It cannot be used apart from the activity. If the activity is stopped then the fragment
cannot be started and if the Activity is destroyed all fragments inside that activity are
destroyed automatically.

Fragment has its own layout (user interface)

It is also possible to create Fragment without user interface.

To create a new Fragment: right clicking on the java folder and select New->Fragment
Lab5, Android Studio Fragments (MyFragments project)

A Fragment is a little similar to the Activity and it has its own life cycle.

The Fragment always render inside the Activity

You can add <fragment> inside the layout file of the activity and specifies the properties:
<fragment android:name="com.example.fragment. InformationFragment "
android:layout_height="match_parent" android:layout_width="match_parent">
</fragment>

The layout designing for the fragment is same as for Activity.

An Activity hosting a Fragment can send data to and receive data from the Fragment.

Recommendation: A Fragment can't communicate directly with another


Fragment, even within the same Activity. The host Activity must be
used as an intermediary.

In order for an activity to communicate with a fragment, the activity must identify the fragment
object via the ID assigned to it using the findViewById() method.
Lab5, Android Studio Fragments (MyFragments project)

onCreateView method is called when


Fragment should create its View object
hierarchy (either dynamically or via
XML layout inflation)
Fragments -> Example
Add to MainActivity a method to select fragments:

? DEFAULT fragment is… ?

if button1 is pressed
Return the FragmentManager for
interacting with fragments
associated with this MainActivity.

the fragment is placed in


a FragmentTransaction

For the given container view


id, we can replace existing
fragment by new given
fragment.
Fragments
Sending Data to Fragment from Activity
In Activity
One way to get data from activity is by calling a method on the activity that returns
data as shown above. Data can also be sent to fragment when it is created by adding
data to bundle:

Bundle bundle = new Bundle();


bundle.putString("user", "user name");
SampleFragment fragment = new SampleFragment();
fragment.setArguments(bundle);

In Fragment
Read the data in onCreateView method of the fragment by calling getArguments()
method to get the bundle and calling appropriate methods on it to read values from it.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle


savedInstanceState) {
String user = getArguments().getString("user");
return inflater.inflate(R.layout.fragment, container, false);
}
Fragments
Create java class for fragment (FragmentThree.java):
Fragments
If you want to use a fragment for two different action (strings):
Lab5, Android Studio Fragments (MyFragments project)

Infinite
loop ?

Warning
A fragment can not call a fragment
Toast.makeText(getContext(),"Fragment No. 1",
No builing errors
Toast.LENGTH_LONG).show();
In an unpredictable way, works
Lab5, Android Studio Fragments MainActivity.java
package com.example.myfragments; {
if(view == findViewById(R.id.button4)){
import android.os.Bundle; Bundle bundle = new Bundle();
import android.view.View; bundle.putString("param", "21");
import android.app.Activity; fr = new FragmentThree();
import android.app.Fragment; fr.setArguments(bundle);
import android.app.FragmentManager; }
import android.app.FragmentTransaction; else
import android.widget.TextView; if(view == findViewById(R.id.button5)){
fr = new FragmentFour();
public class MainActivity extends Activity { }
protected void onCreate(Bundle savedInstanceState) { else
super.onCreate(savedInstanceState); fr = new FragmentOne();
setContentView(R.layout.activity_main); }
} }
public void selectFrag(View view) { FragmentManager fm = getFragmentManager();
Fragment fr; FragmentTransaction fragmentTransaction =
if(view == findViewById(R.id.button2)) { fm.beginTransaction();
fr = new FragmentTwo();
}else fragmentTransaction.replace(R.id.fragment_place,
{ fr);
if(view == findViewById(R.id.button3)){ fragmentTransaction.commit();
Bundle bundle = new Bundle(); }
bundle.putString("param", }
"3543543543534");
fr = new FragmentThree();
fr.setArguments(bundle);
}
else
Lab5, Android Studio Fragments activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout <Button
xmlns:android="http://schemas.android.com/apk/res/a android:id="@+id/button1"
ndroid" android:layout_width="285dp"
xmlns:tools="http://schemas.android.com/tools" android:layout_height="wrap_content"
android:layout_width="match_parent" android:lineSpacingExtra="30sp"
android:layout_height="match_parent" android:lineSpacingMultiplier="3"
android:orientation="vertical" > android:onClick="selectFrag"
android:text="Fragment No.1" />
<Button
android:id="@+id/button3" <Button
android:layout_width="283dp" android:id="@+id/button2"
android:layout_height="wrap_content" android:layout_width="288dp"
android:onClick="selectFrag" android:layout_height="wrap_content"
android:text="Fragment No.3 - FIRST string" /> android:onClick="selectFrag"
android:text="Fragment No.2" />
<Button
android:id="@+id/button4" <fragment
android:layout_width="288dp" android:id="@+id/fragment_place"
android:layout_height="wrap_content"
android:onClick="selectFrag" android:name="com.example.myfragments.FragmentOne"
android:text="Fragment No.3 - SECOND string" /> android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button5" </LinearLayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.4 -> Fragment No. 1" />
Lab5, Android Studio Fragments FragmentOne.java

ackage com.example.myfragments;

import android.app.Fragment;
//import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class FragmentOne extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {

//Inflate the layout for this fragment


Toast.makeText(getContext(),"Fragment No. 1",Toast.LENGTH_LONG).show();
return inflater.inflate(
R.layout.fragment_one, container, false);
Lab5, Android Studio Fragments Fragment_one.xml
?xml version="1.0" encoding="utf-8"?>
<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#ff0088">

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="This is fragment No.1"
android:textAllCaps="false"
android:textSize="36sp"
android:textStyle="bold|italic" />

</LinearLayout>
Students’ task
An app that compute “something” using a
fragment

You might also like