uopspop

Untitled

Sep 27th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. package com.example.sam.myfragmentprac;
  2.  
  3. import android.app.Fragment;
  4. import android.support.v4.app.FragmentManager;
  5. import android.support.v4.app.FragmentTransaction;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Toast;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.  
  13.     private static final String TAG = "fragmentTAG";
  14.  
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_main);
  19.     }
  20.  
  21.     public void onAddClick(View view){
  22.         // we need to new a FragmentTransaction every time when we need to do something
  23.         FragmentManager fragmentManager = this.getSupportFragmentManager();
  24.         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  25.         //
  26.         android.support.v4.app.Fragment fragment = fragmentManager.findFragmentById(R.id.myFrameLayoutContainer); //
  27.         if (fragment == null){
  28.             // prepare data
  29.             String title = "Fragment A";
  30.             MyFragment fragmentA = new MyFragment();
  31.             Bundle bundle = new Bundle();
  32.             bundle.putString("title",title);
  33.             fragmentA.setArguments(bundle);
  34.             //// real action - transaction
  35.             //    將Fragment物件放到R.id.myFrameLayoutContainer這個容器上(創造Fragment+Attach它到容器上)
  36.             fragmentTransaction.add(R.id.myFrameLayoutContainer, fragmentA, TAG);
  37.             fragmentTransaction.commit();
  38.         }else{
  39.             showToast("fragment exists.");
  40.         }
  41.     }
  42.  
  43.     public void onReplaceClick(View view){
  44.         // we need to new a FragmentTransaction every time when we need to do something
  45.         FragmentManager fragmentManager = this.getSupportFragmentManager();
  46.         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  47.         //
  48.         MyFragment fragmentB = new MyFragment();
  49.         Bundle bundle = new Bundle();
  50.         bundle.putString("title","fragment B");
  51.         fragmentB.setArguments(bundle);
  52.         fragmentTransaction.replace(R.id.myFrameLayoutContainer,fragmentB,TAG);
  53.         fragmentTransaction.commit();
  54.     }
  55.  
  56.     public void onAttachClick(View view){
  57.         // we need to new a FragmentTransaction every time when we need to do something
  58.         FragmentManager fragmentManager = this.getSupportFragmentManager();
  59.         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  60.         //
  61.         android.support.v4.app.Fragment fragment = fragmentManager.findFragmentById(R.id.myFrameLayoutContainer);
  62.         if (fragment == null){
  63.             showToast("fragment does not exist.");
  64.         }else{
  65.             if (fragment.isDetached()){
  66.                 fragmentTransaction.attach(fragment);
  67.                 fragmentTransaction.commit();
  68.             }else{
  69.                 showToast("fragment already attached");
  70.             }
  71.         }
  72.     }
  73.  
  74.     public void onDettachClick(View view){
  75.         // we need to new a FragmentTransaction every time when we need to do something
  76.         FragmentManager fragmentManager = this.getSupportFragmentManager();
  77.         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  78.         //
  79.         android.support.v4.app.Fragment fragment = fragmentManager.findFragmentById(R.id.myFrameLayoutContainer);
  80.         if (fragment == null){
  81.             showToast("fragment does not exist.");
  82.         }else{
  83.             if (!fragment.isDetached()){
  84.                 fragmentTransaction.detach(fragment);
  85.                 fragmentTransaction.commit();
  86.             }else{
  87.                 showToast("fragment already dettached");
  88.             }
  89.         }
  90.     }
  91.  
  92.     public void onRemoveClick(View view){
  93.         // we need to new a FragmentTransaction every time when we need to do something
  94.         FragmentManager fragmentManager = this.getSupportFragmentManager();
  95.         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  96.         //
  97.         android.support.v4.app.Fragment fragment = fragmentManager.findFragmentById(R.id.myFrameLayoutContainer);
  98.         if (fragment == null){
  99.             showToast("fragment does not exist");
  100.         }else{
  101.             fragmentTransaction.remove(fragment);
  102.             fragmentTransaction.commit();
  103.         }
  104.     }
  105.  
  106.     public void onFinishClick(View view){
  107.         finish();
  108.     }
  109.  
  110.     private void showToast(String msg){
  111.         Toast.makeText(getApplicationContext(),
  112.                 msg,
  113.                 Toast.LENGTH_SHORT)
  114.                 .show();
  115.     }
  116. }
Add Comment
Please, Sign In to add comment