2015年8月31日 星期一

Android-addFragment replaceFragment


BaseActivity.java
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class BaseActivity extends Activity {

    
    private String TAG = "Fragment123";
    private String tag;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getFragmentManager();
        // init
        Fragment fragment1=new FristFragment();
        replaceFragment(fragment1);

    }

    // 取代Fragment 是無上一頁的
    public void replaceFragment(Fragment fragment){

        if(fragmentManager.getBackStackEntryCount()>0){
            fragmentManager.popBackStack();
            fragmentManager.executePendingTransactions();
        }
        
        tag = TAG + (fragmentManager.getBackStackEntryCount()> 
                0?fragmentManager.getBackStackEntryCount()-1:0);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.linearLayout_01, fragment, tag);
        fragmentTransaction.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }

    // 堆疊Fragment
    public void addFragment(Fragment fragment){
        
        tag = TAG + fragmentManager.getBackStackEntryCount();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.linearLayout_01, fragment, tag);
        fragmentTransaction.addToBackStack(tag);
        fragmentTransaction.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }
    
    @Override
    public void onBackPressed() {
        if(fragmentManager.getBackStackEntryCount() != 0) {
            fragmentManager.popBackStack();
        } else {
            super.onBackPressed();
        }
    }

}

BaseFragment.java
import android.app.Fragment;
import android.os.Bundle;
import android.widget.TextView;

public class BaseFragment extends Fragment{

    private TextView text_titleName;
    
    public TextView getText_titleName() {
        return text_titleName;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        text_titleName = (TextView)getActivity().findViewById(R.id.textView1);
        
    }
    
    public void replaceFragment(Fragment fragment) {

        try{
            MainActivity activity = (MainActivity) getActivity();
            activity.replaceFragment(fragment);
        } catch (Exception e){

        }
    }
    
    public void addFragment(Fragment fragment) {

        try{
            MainActivity activity = (MainActivity) getActivity();
            activity.addFragment(fragment);
        } catch (Exception e){

        }
    }
    
}


沒有留言:

張貼留言