Hello Friends,
Today I am going to share code for how to create android fragment and how to handle back stack. Have a look at below code or you can download full code from GIT server.
1)LoginActivity.java-
Today I am going to share code for how to create android fragment and how to handle back stack. Have a look at below code or you can download full code from GIT server.
1)LoginActivity.java-
package com.androidhub4you.fragmentdemo;
|
|||||||||||||||||||||||||||||||||||||||
import android.content.Intent;
|
|||||||||||||||||||||||||||||||||||||||
import android.os.Bundle;
|
|||||||||||||||||||||||||||||||||||||||
import android.support.v7.app.ActionBarActivity;
|
|||||||||||||||||||||||||||||||||||||||
import android.view.View;
|
|||||||||||||||||||||||||||||||||||||||
public class LoginActivity extends ActionBarActivity {
|
|||||||||||||||||||||||||||||||||||||||
@Override
|
|||||||||||||||||||||||||||||||||||||||
protected void onCreate(Bundle savedInstanceState) {
|
|||||||||||||||||||||||||||||||||||||||
super.onCreate(savedInstanceState);
|
|||||||||||||||||||||||||||||||||||||||
setContentView(R.layout.activity_login);
|
|||||||||||||||||||||||||||||||||||||||
findViewById(R.id.button__activity_main).setOnClickListener(new View.OnClickListener() {
|
|||||||||||||||||||||||||||||||||||||||
@Override
|
|||||||||||||||||||||||||||||||||||||||
public void onClick(View view) {
|
|||||||||||||||||||||||||||||||||||||||
Intent mIntent=new Intent(LoginActivity.this,MainActivity.class);
|
|||||||||||||||||||||||||||||||||||||||
startActivity(mIntent);
|
|||||||||||||||||||||||||||||||||||||||
}
|
|||||||||||||||||||||||||||||||||||||||
});
|
|||||||||||||||||||||||||||||||||||||||
}
|
|||||||||||||||||||||||||||||||||||||||
}
2)activity_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
3)MainActivity.java-
package com.androidhub4you.fragmentdemo; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; /** * Manish Srivastava */ public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.v("<<main activity>>", "<<screen>>"); //display home back button ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); //In side Create method of MainActivity we will add main fragment FragmentTransaction transaction = getFragmentManager().beginTransaction(); FragmentMain fragmentMain = new FragmentMain(); transaction.addToBackStack("fragmentMain"); transaction.replace(R.id.container, fragmentMain); transaction.commit(); } @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) { switch (item.getItemId()) { case android.R.id.home: handleBackStack(); break; case R.id.action_settings: //do nothing break; } return super.onOptionsItemSelected(item); } //handle back stack from hardware back button and top action bar home back button public boolean handleBackStack() { //on back press check the count of fragment in stack int count = getFragmentManager().getBackStackEntryCount(); Log.v("<<Fragment in stack>>",String.valueOf(count)); if (count == 1) { //if count is 0 then finish main activity finish(); }else { //else remove one fragment from the stack FragmentTransaction transaction = getFragmentManager().beginTransaction(); getFragmentManager().popBackStackImmediate(); transaction.commit(); } return true; } //set action bar title name public void setActionBarTitle(String title) { getSupportActionBar().setTitle(title); } //handle the hardware back press @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ handleBackStack(); return true; } return super.onKeyDown(keyCode, event); } }4)activity_main.xml-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
5)FragmentMain.java-
package com.androidhub4you.fragmentdemo; import android.app.FragmentTransaction; import android.os.Bundle; import android.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; /** * Manish Srivastava */ public class FragmentMain extends Fragment { public FragmentMain() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_main, container, false); final Button mButtonFragmentMain=(Button)view.findViewById(R.id.button__fragment_main); Log.v("<< main fragment>>","<<screen>>"); mButtonFragmentMain.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //call the child fragment FragmentTransaction transaction = getFragmentManager().beginTransaction(); FragmentChild fragmentChild = new FragmentChild(); transaction.addToBackStack("fragmentChild"); transaction.replace(R.id.container, fragmentChild); transaction.commit(); } }); //call the main activity set tile method ((MainActivity)getActivity()).setActionBarTitle("Home Fragment"); return view; } }
6)fragment_main.xml-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="Main Fragment Screen" android:textSize="26sp" android:textStyle="bold" /> <Button android:id="@+id/button__fragment_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="Open Child Fragment" android:textSize="20sp" /> </RelativeLayout>
7)FragmentChild.java-
package com.androidhub4you.fragmentdemo; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Manish Srivastava */ public class FragmentChild extends Fragment { public FragmentChild() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_child, container, false); //call the main activity set tile method ((MainActivity)getActivity()).setActionBarTitle("Child Fragment"); return view; } }
8)fragment_child.xml-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Child Fragment Screen" android:textSize="26sp" android:textStyle="bold" /> </RelativeLayout>
DOWNLOAD FULL CODE
Thanks!
Manish