Hello friends
Today we are learn about fragment methods most of developer can't handle proper fragment navigation so i found best solution and proper navigation of fragment.If you learn this post i am sure you feel relax with fragment navigation. So lets start.....
First you create a new android project.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.samset.user.fragmentbackprocesssample.MainActivity">
<LinearLayout
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/primary_dark"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Fragment Navigation"
android:textColor="#fff" />
</LinearLayout>
<FrameLayout
android:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tv"></FrameLayout>
</RelativeLayout>
MainActvity.java
package com.samset.user.fragmentbackprocesssample;
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.os.Bundle;
import android.util.Log;
import com.samset.user.fragmentbackprocesssample.fragments.HomeFragment;
import com.samset.user.fragmentbackprocesssample.fragments.LoginFragment;
public class MainActivity extends AppCompatActivity {
String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragTransaction(new LoginFragment());
}
@Override
protected void onResume() {
super.onResume();
}
public void fragTransaction(Fragment frag) {
if (frag.isAdded()) {
Log.e(TAG, " fragment add");
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.mainContainer, frag).addToBackStack(null).commit();
} else {
Log.e(TAG, " fragment replace");
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.mainContainer, frag).addToBackStack(null).commit();
}
}
@Override
public void onBackPressed() {
// here you simply back handle you check stack count and then enable backpress button
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
Log.e(TAG, " fragment popback ");
} else {
super.onBackPressed();
}
}
}
HomeFragment.java
package com.samset.user.fragmentbackprocesssample.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.AppCompatButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.samset.user.fragmentbackprocesssample.R;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
private AppCompatButton btnProfile,btnDetails;
public HomeFragment() {
// 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_home, container, false);
btnProfile= (AppCompatButton) view.findViewById(R.id.btnprofile);
btnDetails= (AppCompatButton) view.findViewById(R.id.btndetails);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction(); //If you add tag name the you can navigate anywhere from any frgament
transaction.replace(R.id.mainContainer,new ProfileFragment()).addToBackStack("home").commit();
}
});
btnDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction(); //If you add tag name the you can navigate anywhere from any frgament
transaction.replace(R.id.mainContainer,new DetailsFragment()).addToBackStack("home").commit();
}
});
}
}
Today we are learn about fragment methods most of developer can't handle proper fragment navigation so i found best solution and proper navigation of fragment.If you learn this post i am sure you feel relax with fragment navigation. So lets start.....
First you create a new android project.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.samset.user.fragmentbackprocesssample.MainActivity">
<LinearLayout
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/primary_dark"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Fragment Navigation"
android:textColor="#fff" />
</LinearLayout>
<FrameLayout
android:id="@+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tv"></FrameLayout>
</RelativeLayout>
MainActvity.java
package com.samset.user.fragmentbackprocesssample;
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.os.Bundle;
import android.util.Log;
import com.samset.user.fragmentbackprocesssample.fragments.HomeFragment;
import com.samset.user.fragmentbackprocesssample.fragments.LoginFragment;
public class MainActivity extends AppCompatActivity {
String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragTransaction(new LoginFragment());
}
@Override
protected void onResume() {
super.onResume();
}
public void fragTransaction(Fragment frag) {
if (frag.isAdded()) {
Log.e(TAG, " fragment add");
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.mainContainer, frag).addToBackStack(null).commit();
} else {
Log.e(TAG, " fragment replace");
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.mainContainer, frag).addToBackStack(null).commit();
}
}
@Override
public void onBackPressed() {
// here you simply back handle you check stack count and then enable backpress button
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
Log.e(TAG, " fragment popback ");
} else {
super.onBackPressed();
}
}
}
HomeFragment.java
package com.samset.user.fragmentbackprocesssample.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.AppCompatButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.samset.user.fragmentbackprocesssample.R;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
private AppCompatButton btnProfile,btnDetails;
public HomeFragment() {
// 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_home, container, false);
btnProfile= (AppCompatButton) view.findViewById(R.id.btnprofile);
btnDetails= (AppCompatButton) view.findViewById(R.id.btndetails);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction(); //If you add tag name the you can navigate anywhere from any frgament
transaction.replace(R.id.mainContainer,new ProfileFragment()).addToBackStack("home").commit();
}
});
btnDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction(); //If you add tag name the you can navigate anywhere from any frgament
transaction.replace(R.id.mainContainer,new DetailsFragment()).addToBackStack("home").commit();
}
});
}
}
Thank you
FullSourceCodeFragmentPopBackStack
Live Sample
No comments:
Post a Comment