.
203 Android Studio Replace Fragments
This tutorial demonstrates how to replace a fragment (main) with another fragment (sub).
1) START UP
Follow the previous tutorial or download startup file here.
It is recommended that you follow and understand the steps in the previous tutorial before attempting this tutorial.
2) DUPLICATE FILES
2.1) Duplicate fragment_main.xml as fragment_sub.xml
Edit as follows (delete the EditText and Button view objects)
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="horizontal">
<TextView android:text="This is Sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
|
2.2) Duplicate MainActivityFragment.java as SubActivityFragment.java
Edit as follows (Delete all methods except onCreateView() and SubActivityFragment() and change the layout reference to fragment_sub)
package com.notarazi.myfragments2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class SubActivityFragment extends Fragment {
public SubActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_sub, container, false);
}
}
|
2.3) Edit MainActivity.java
(We want the application to display the second fragment when the user tap the Action Button)
package com.notarazi.myfragments2;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setFragment(new MainActivityFragment());
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_send) {
//Call setFragment to replace the fragment
setFragment(new SubActivityFragment());
return true;
}
return super.onOptionsItemSelected(item);
}
protected void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.FragmentContainer, fragment);
fragmentTransaction.commit();
}
}
|
OUTCOME.
At this stage, if the user presses Back Button after second fragment is displayed, Android System will return to Home Screen.
We can add a flag to set the BackStack property so that when the user presses Back Button, the application returns to the first fragment.
|
MainActivity.java
package com.notarazi.myfragments2;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setFragment(new MainActivityFragment(),0);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_send) {
setFragment(new SubActivityFragment(), 1);
return true;
}
return super.onOptionsItemSelected(item);
}
protected void setFragment(Fragment fragment, int backstackyes) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (backstackyes==1) {fragmentTransaction.addToBackStack(null);}
fragmentTransaction.replace(R.id.FragmentContainer, fragment);
fragmentTransaction.commit();
}
}
|
OUTCOME.
-
Tap the Action Button. (The user sees the second fragment)
Tap the Back Button. (The user gets back the first fragment)
DOWNLOAD
.