Tuesday, November 15, 2016

Fragment in Android

public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";

    @Override 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.v(TAG, "<<screen>>");


        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        FragmentMain fragmentMain = new FragmentMain();
        transaction.addToBackStack("fragmentMain");
        transaction.replace(R.id.container, fragmentMain);
        transaction.commit();

    }


    public boolean handleBackStack() {
        int count = getFragmentManager().getBackStackEntryCount();
        Log.v(TAG, String.valueOf(count));
        if (count == 1) {
            finish();
        } else {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            getFragmentManager().popBackStackImmediate();
            transaction.commit();
        }
        return true;
    }


    //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);
    }
}
public class FragmentMain extends Fragment {


    public FragmentMain() {
        // Required empty public constructor    }

    @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        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) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                FragmentChild fragmentChild = new FragmentChild();
                transaction.addToBackStack("fragmentChild");
                transaction.replace(R.id.container, fragmentChild);
                transaction.commit();

            }
        });


        return view;
    }
}
public class FragmentChild extends Fragment {


    public FragmentChild() {
        // Required empty public constructor    }


    @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_child, container, false);



        return view;
    }
}
activity_main
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/container" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical">

</FrameLayout>
 
fragment_main
 
<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>
 
fragment_child
<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>

   

No comments:

Post a Comment