Lab5 PDM
Lab5 PDM
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.
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.
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>
An Activity hosting a Fragment can send data to and receive data from the Fragment.
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)
if button1 is pressed
Return the FragmentManager for
interacting with fragments
associated with this MainActivity.
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.
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;
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