当你的项目中使用fragment的replace的时候,如何正确使用返回键去规避fragment重叠

package com.gbkj.management;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import cn.gbkj.management.R;

import com.gbkj.management.fragment.FunctionFragment1;
import com.gbkj.management.fragment.FunctionFragment2;
import com.gbkj.management.fragment.FunctionFragment3;
import com.gbkj.management.fragment.FunctionFragment4;
import com.gbkj.management.fragment.FunctionFragment5;

public class MainActivity extends FragmentActivity implements OnCheckedChangeListener {

    private List<String> mDatas;
    private FrameLayout fl_main;
    private TextView tv_title;
    private ArrayList<Fragment> fragments;
    private RadioGroup rg_main;
    
    private Button bt_return;
    public Fragment currentFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        fl_main=(FrameLayout)findViewById(R.id.fl_main);
        tv_title=(TextView)findViewById(R.id.tv_title);
        rg_main=(RadioGroup)findViewById(R.id.rg_main);
        init();
        
    }
    
    @Override
    protected void onResume() {
        super.onPause();
        rg_main.setOnCheckedChangeListener(this);
        rg_main.check(R.id.rb_mytask);
    }
    /**
     * 修改头部文本的接口方法
     * @param text
     */
    public void setTitleText(String text){
        tv_title.setText(text);
    
    }

    private void init() {
        mDatas=new ArrayList<String>();
        mDatas.add("我的任务");
        mDatas.add("我的留言");
        mDatas.add("待收任务");
        mDatas.add("请求协助");
        mDatas.add("我的同事");
        
        fragments=new ArrayList<Fragment>();
        fragments.add(new FunctionFragment1());
        fragments.add(new FunctionFragment2());
        fragments.add(new FunctionFragment3());
        fragments.add(new FunctionFragment4());
        fragments.add(new FunctionFragment5());
    }
    
    
    /*    
    @Override
    public void onItemClick(AdapterView<?> parent, View convertView, final int position, long arg3) {
            //fl_main.removeAllViews();
            adapter.setCurentPosition(position);
            tv_title.setText(mDatas.get(position));
            new Thread(new Runnable() {
                @Override
                public void run() {
                    switchFragment(fragments.get(position));
                }
            }).start();
    }*/
    
    
    //返回按钮
    public void back(View v){
        startActivity(new Intent(this,LoginActivity.class));
    }
    
    //替换fragment
    public void switchFragment(Fragment fragment,String tag){
        fl_main.removeAllViews();
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_main, fragment,tag).commit();
    }
    
    
    @Override
    public void onCheckedChanged(RadioGroup arg0, int checkId) {
        switch (checkId) {
        case R.id.rb_mytask:
            tv_title.setText(mDatas.get(0));
            switchFragment(fragments.get(0),"function1");
            break;
        case R.id.rb_message:
            tv_title.setText(mDatas.get(1));
            switchFragment(fragments.get(1),"function2");
            break;
        case R.id.rb_task_coming:
            tv_title.setText(mDatas.get(2));
            switchFragment(fragments.get(2),"function3");
            break;
        case R.id.rb_help:
            tv_title.setText(mDatas.get(3));
            switchFragment(fragments.get(3),"function4");
            break;
        case R.id.rb_workmate:
            tv_title.setText(mDatas.get(4));
            switchFragment(fragments.get(4),"function5");
            break;    
        default:
            break;
        }
    }
    
    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {

        if(keyCode==KeyEvent.KEYCODE_BACK){
        //我觉得参考的朋友们有必要想想我这里为什么要开一个线程
            if(fragments.contains(currentFragment)){
                new Thread(new Runnable() {
                    
                    @Override
                    public void run() {
                        finish();
                    }
                }).start();
                
            }else{
                return super.onKeyDown(keyCode, event);
            }
        }
        return true;
    }
}


你可能感兴趣的:(Fragment,返回键)