Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.sam.myfragmentprac;
- import android.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.view.View;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- private static final String TAG = "fragmentTAG";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void onAddClick(View view){
- // we need to new a FragmentTransaction every time when we need to do something
- FragmentManager fragmentManager = this.getSupportFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- //
- android.support.v4.app.Fragment fragment = fragmentManager.findFragmentById(R.id.myFrameLayoutContainer); //
- if (fragment == null){
- // prepare data
- String title = "Fragment A";
- MyFragment fragmentA = new MyFragment();
- Bundle bundle = new Bundle();
- bundle.putString("title",title);
- fragmentA.setArguments(bundle);
- //// real action - transaction
- // 將Fragment物件放到R.id.myFrameLayoutContainer這個容器上(創造Fragment+Attach它到容器上)
- fragmentTransaction.add(R.id.myFrameLayoutContainer, fragmentA, TAG);
- fragmentTransaction.commit();
- }else{
- showToast("fragment exists.");
- }
- }
- public void onReplaceClick(View view){
- // we need to new a FragmentTransaction every time when we need to do something
- FragmentManager fragmentManager = this.getSupportFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- //
- MyFragment fragmentB = new MyFragment();
- Bundle bundle = new Bundle();
- bundle.putString("title","fragment B");
- fragmentB.setArguments(bundle);
- fragmentTransaction.replace(R.id.myFrameLayoutContainer,fragmentB,TAG);
- fragmentTransaction.commit();
- }
- public void onAttachClick(View view){
- // we need to new a FragmentTransaction every time when we need to do something
- FragmentManager fragmentManager = this.getSupportFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- //
- android.support.v4.app.Fragment fragment = fragmentManager.findFragmentById(R.id.myFrameLayoutContainer);
- if (fragment == null){
- showToast("fragment does not exist.");
- }else{
- if (fragment.isDetached()){
- fragmentTransaction.attach(fragment);
- fragmentTransaction.commit();
- }else{
- showToast("fragment already attached");
- }
- }
- }
- public void onDettachClick(View view){
- // we need to new a FragmentTransaction every time when we need to do something
- FragmentManager fragmentManager = this.getSupportFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- //
- android.support.v4.app.Fragment fragment = fragmentManager.findFragmentById(R.id.myFrameLayoutContainer);
- if (fragment == null){
- showToast("fragment does not exist.");
- }else{
- if (!fragment.isDetached()){
- fragmentTransaction.detach(fragment);
- fragmentTransaction.commit();
- }else{
- showToast("fragment already dettached");
- }
- }
- }
- public void onRemoveClick(View view){
- // we need to new a FragmentTransaction every time when we need to do something
- FragmentManager fragmentManager = this.getSupportFragmentManager();
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- //
- android.support.v4.app.Fragment fragment = fragmentManager.findFragmentById(R.id.myFrameLayoutContainer);
- if (fragment == null){
- showToast("fragment does not exist");
- }else{
- fragmentTransaction.remove(fragment);
- fragmentTransaction.commit();
- }
- }
- public void onFinishClick(View view){
- finish();
- }
- private void showToast(String msg){
- Toast.makeText(getApplicationContext(),
- msg,
- Toast.LENGTH_SHORT)
- .show();
- }
- }
Add Comment
Please, Sign In to add comment