Android studio: A Fragment represents a behavior or a portion of a user interfac
ID: 3589934 • Letter: A
Question
Android studio:
A Fragment represents a behavior or a portion of a user interface in an activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities). The main goal of this lab is to learn how to use fragments in Android Applications.
Combine multiple fragments in a single activity.
Manage the life cycle of activities and fragments properly.
Implement fragment communications.
Use bundle objects to save state data to preserve the current UI state
you will develop a Tour guide App. The app introduces the basic information about Canadian cities to the user. The app has one activity and two fragments, as shown in Fig. 1. When a user clicks on a city name in the top list fragment, the description appears in the second fragment. The app uses a bundle object to save state data to preserve the current UI state so that it doesn’t change when users rotate the device.
Explanation / Answer
******************* file : fragment_main.xml ************************
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.example.fragmenttransaction.MainActivityFragment">
<TextView
style="@style/lableStyle"
android:text="@string/lable_in_mainfragment" />
<Button
android:id="@+id/button01"
style="@style/buttonStyle"
android:text="@string/lable_move_fragment01" />
<Button
android:id="@+id/button02"
style="@style/buttonStyle"
android:text="@string/lable_move_fragment02" />
<Button
android:id="@+id/button03"
style="@style/buttonStyle"
android:text="@string/lable_move_fragment03" />
</LinearLayout>
*********** ***** file : MainActivityFragment.java *********************
package com.example.fragmenttransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainActivityFragment extends Fragment implements View.OnClickListener {
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
view.findViewById(R.id.button01).setOnClickListener(this);
view.findViewById(R.id.button02).setOnClickListener(this);
view.findViewById(R.id.button03).setOnClickListener(this);
return view;
}
@Override
public void onResume() {
super.onResume();
((MainActivity) getActivity()).disableNavigationIcon();
((MainActivity) getActivity()).setToolbarTitle(R.string.MainFragmentTitle);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button01:
//Move to Fragment 01
((MainActivity) getActivity()).showFragment(new FragmentExample1());
break;
case R.id.button02:
//Move to Fragment 02
((MainActivity) getActivity()).showFragment(new FragmentExample2());
break;
case R.id.button03:
//Move to Fragment 03
((MainActivity) getActivity()).showFragment(new FragmentExample3());
break;
}
}
}
*********************************** file : fragment_example_1.xml **********************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light"
android:gravity="center"
android:orientation="vertical">
<TextView
style="@style/lableStyle"
android:text="@string/lable_in_fragment_01"
android:textColor="@android:color/white" />
<Button
android:id="@+id/btn_f1_01"
style="@style/buttonStyle"
android:text="@string/lable_move_fragment02" />
<Button
android:id="@+id/btn_f1_02"
style="@style/buttonStyle"
android:text="@string/lable_move_fragment03" />
</LinearLayout>
*********************** file : FragmentExample1.java ********************
package com.example.fragmenttransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentExample1 extends Fragment implements View.OnClickListener {
public FragmentExample1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_example_1, container, false);
view.findViewById(R.id.btn_f1_01).setOnClickListener(this);
view.findViewById(R.id.btn_f1_02).setOnClickListener(this);
return view;
}
@Override
public void onResume() {
super.onResume();
((MainActivity) getActivity()).setToolbarTitle(R.string.Fragment01Title);
((MainActivity) getActivity()).enableNavigationIcon();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_f1_01:
//Move to Fragment 02
((MainActivity) getActivity()).showFragment(new FragmentExample2());
break;
case R.id.btn_f1_02:
//Move to Fragment 03
((MainActivity) getActivity()).showFragment(new FragmentExample3());
break;
}
}
}
********************** file : activity_main.xml *******************
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.fragmenttransaction.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
******************************* file : content_main.xml ***********************
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
***************** file : MainActivity.java **********************
package com.example.fragmenttransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
showFragment(new MainActivityFragment());
}
protected void showFragment(Fragment fragment) {
String TAG = fragment.getClass().getSimpleName();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.mainContainer, fragment, TAG);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
}
protected void backstackFragment() {
Log.d("Stack count", getSupportFragmentManager().getBackStackEntryCount() + "");
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
finish();
}
getSupportFragmentManager().popBackStack();
removeCurrentFragment();
}
private void removeCurrentFragment() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment currentFrag = getSupportFragmentManager()
.findFragmentById(R.id.mainContainer);
if (currentFrag != null) {
transaction.remove(currentFrag);
}
transaction.commitAllowingStateLoss();
}
@Override
public void onBackPressed() {
super.onBackPressed();
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
finish();
}
}
protected void enableNavigationIcon() {
toolbar.setNavigationIcon(R.mipmap.back_arrow);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
backstackFragment();
}
});
}
protected void disableNavigationIcon() {
toolbar.setNavigationIcon(null);
}
protected void setToolbarTitle(int resID) {
toolbar.setTitle(resID);
}
}