0

这是一个主界面,顶部是Fragment,底部四个RadioButton.当我打开主界面然后选中最后一个"我的"模块,然后直接按home键返回手机桌面.但是过几分钟点击打开APP会错乱:RadioButton选中的是"我的",但是Fragment选中的确是第一个,应该是内存回收导致的,请问这样的问题该怎么解决呢?
截图和代码:
图片描述


/**
 * 主界面
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {
    
    @BindView(R.id.rb_home)
    RadioButton mHome;
    @BindView(R.id.rb_channel)
    RadioButton mChannel;
    @BindView(R.id.rb_discovery)
    RadioButton mDiscovery;
    @BindView(R.id.rb_mine)
    RadioButton mMine;
    @BindView(R.id.rg_tabs)
    RadioGroup mTabs;
    @BindView(R.id.iv_switch_game_normal)
    ImageView mSwitchGameNormal;
    @BindView(R.id.iv_close)
    ImageView mClose;

    private int currentButton = -1;
    private static final int REQUEST_EXTERNAL_STORAGE = 3;
    FragmentManager manager;
    FragmentTransaction transaction;
    private PopupWindow mPopupWindow;

    private static String[] PERMISSIONS_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private String mLocalVersion;
    private String mToken;
    private String logoutFailMsg;
    private RotateAnimation mRotateAnimation;
    private HomeFragment homeFragment;
    private ChannelFragment channelFragment;
    private DiscoveryFragment discoveryFragment;
    private MineFragment mineFragment;

    @Override
    protected View initView() {
        View view = View.inflate(this, R.layout.activity_main, null);
        ButterKnife.bind(this, view);
        mIvMore.setImageResource(R.mipmap.search);
        mTabs.check(R.id.rb_home);//默认选中首页
        mIvBack.setVisibility(View.GONE);
        frameLayout.addView(view);
        return view;
    }

    @Override
    protected void initData() {
//        NetWorkStatusUtils.checkNetWork(this);//当前网络状态检查
        int localVersionCode = PackageUtils.getVersionCode(this);
        mLocalVersion = String.valueOf(localVersionCode);//本地版本号
        Log.d(TAG, "initData: 1055=" + mLocalVersion);

        //获取token
        mToken = PreferenceUtils.getString(this, "token");
        showHomePage(); //打开界面即显示首页内容
        startService(new Intent(this, NetworkWatchService.class));
    }

    @Override
    protected void initEvent() {
        mHome.setOnClickListener(this);
        mChannel.setOnClickListener(this);
        mDiscovery.setOnClickListener(this);
        mMine.setOnClickListener(this);
        mIvMore.setOnClickListener(this);
        mTvTitle.setOnClickListener(this);
        mSwitchGameNormal.setOnClickListener(this);
    }


    /**
     * 打开界面即显示首页内容
     */
    private void showHomePage() {
        mTabs.check(R.id.rb_home);

        //TODO:代码待优化
        int homeButton = 0;//避免选中模块后重复点击
        if (currentButton == homeButton) {
            return;
        }
        mTvTitle.setText("首页");
        mIvMore.setVisibility(View.VISIBLE);
        mIvMore.setImageResource(R.mipmap.search);
        mMainTitle.setVisibility(View.VISIBLE);
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        mTvTitle.setTextColor(ActivityCompat.getColor(this, R.color.cl_default));
        mToolbar.setBackgroundColor(ActivityCompat.getColor(this, R.color.colorWhite));
        homeFragment = (HomeFragment) getFragmentManager().findFragmentByTag("tab1");
        channelFragment = (ChannelFragment) getFragmentManager().findFragmentByTag("tab2");
        discoveryFragment = (DiscoveryFragment) getFragmentManager().findFragmentByTag("tab3");
        mineFragment = (MineFragment) getFragmentManager().findFragmentByTag("tab4");
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
            transaction.add(R.id.fl_container, homeFragment, "tab1");
        }
        if (channelFragment != null)
            transaction.hide(channelFragment);
        if (discoveryFragment != null)
            transaction.hide(discoveryFragment);
        if (mineFragment != null)
            transaction.hide(mineFragment);
        transaction.commit();
        transaction.show(homeFragment);
        currentButton = 0;
    }

    /**
     * 频道
     */
    private void showChannelPage() {
        mTabs.check(R.id.rb_channel);
        int channelButton = 1;//避免选中模块后重复点击
        if (currentButton == channelButton) {
            return;
        }
        mTvTitle.setText("频道");
        mIvTitle.setVisibility(View.GONE);
        mIvMore.setVisibility(View.GONE);
        mIvMore.setImageResource(R.mipmap.search);
        mMainTitle.setVisibility(View.VISIBLE);
        mTvTitle.setTextColor(ActivityCompat.getColor(this, R.color.cl_default));
        mToolbar.setBackgroundColor(ActivityCompat.getColor(this, R.color.colorWhite));
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        homeFragment = (HomeFragment) getFragmentManager().findFragmentByTag("tab1");
        channelFragment = (ChannelFragment) getFragmentManager().findFragmentByTag("tab2");
        discoveryFragment = (DiscoveryFragment) getFragmentManager().findFragmentByTag("tab3");
        mineFragment = (MineFragment) getFragmentManager().findFragmentByTag("tab4");
        if (channelFragment == null) {
            channelFragment = new ChannelFragment();
            transaction.add(R.id.fl_container, channelFragment, "tab2");
        }
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (discoveryFragment != null) {
            transaction.hide(discoveryFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
        transaction.show(channelFragment);
        transaction.commit();
        currentButton = 1;
    }

    /**
     * 发现
     */
    private void showDiscoveryPage() {
        mTabs.check(R.id.rb_discovery);
        int discoveryButton = 2;//避免选中模块后重复点击
        if (currentButton == discoveryButton) {
            return;
        }
        mTvTitle.setText("发现");
        mIvTitle.setVisibility(View.GONE);
        mIvMore.setVisibility(View.GONE);
        mMainTitle.setVisibility(View.VISIBLE);
        mTvTitle.setTextColor(ActivityCompat.getColor(this, R.color.cl_default));
        mToolbar.setBackgroundColor(ActivityCompat.getColor(this, R.color.colorWhite));
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        homeFragment = (HomeFragment) getFragmentManager().findFragmentByTag("tab1");
        channelFragment = (ChannelFragment) getFragmentManager().findFragmentByTag("tab2");
        discoveryFragment = (DiscoveryFragment) getFragmentManager().findFragmentByTag("tab3");
        mineFragment = (MineFragment) getFragmentManager().findFragmentByTag("tab4");
        if (discoveryFragment == null) {
            discoveryFragment = new DiscoveryFragment();
            transaction.add(R.id.fl_container, discoveryFragment, "tab3");
        }
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (channelFragment != null) {
            transaction.hide(channelFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
        transaction.show(discoveryFragment);
        transaction.commit();
        currentButton = 2;
    }

    /**
     * 我的
     */
    private void showMinePage() {
        mTabs.check(R.id.rb_mine);
        int mineButton = 3;//避免选中模块后重复点击
        if (currentButton == mineButton) {
            return;
        }
        mTvTitle.setText("我的");
        mIvMore.setImageResource(R.mipmap.setting);
        mIvMore.setVisibility(View.VISIBLE);
        mTvTitle.setTextColor(ActivityCompat.getColor(this, R.color.colorWhite));
        mToolbar.setBackgroundColor(ActivityCompat.getColor(this, R.color.orange));
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        homeFragment = (HomeFragment) getFragmentManager().findFragmentByTag("tab1");
        channelFragment = (ChannelFragment) getFragmentManager().findFragmentByTag("tab2");
        discoveryFragment = (DiscoveryFragment) getFragmentManager().findFragmentByTag("tab3");
        mineFragment = (MineFragment) getFragmentManager().findFragmentByTag("tab4");
        if (mineFragment == null) {
            mineFragment = new MineFragment();
            transaction.add(R.id.fl_container, mineFragment, "tab4");
        }
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (channelFragment != null)
            transaction.hide(channelFragment);
        if (discoveryFragment != null)
            transaction.hide(discoveryFragment);
        transaction.show(mineFragment);
        transaction.commit();
        currentButton = 3;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb_home:
                showHomePage();
                break;
            case R.id.rb_channel:
                showChannelPage();
                break;
            case R.id.rb_discovery:
                showDiscoveryPage();
                break;
            case R.id.rb_mine:
                showMinePage();
                break;
            case R.id.iv_more:
                if (currentButton == 0) {
                    mIvMore.setImageResource(R.mipmap.search);
                    Intent toSearch = new Intent(MainActivity.this, SearchActivity.class);
                    startActivity(toSearch);
                } else if (currentButton == 3) {
                    Intent toAccountSetting = new Intent(this, AccountSettingActivity.class);
                    startActivity(toAccountSetting);
                }
                break;
            case R.id.iv_switch_game_normal://未弹出window前点击
                mClose.setVisibility(View.VISIBLE);
                mRotateAnimation = new RotateAnimation(0f, 90f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                mRotateAnimation.setDuration(300);
                mClose.setAnimation(mRotateAnimation);
                break;
            default:
                break;
        }
    }
 }

1个回答

0

你应该用一个RadioGroup来管理RadioButton的选中和取消,而不用点击事件来管理,你这样逻辑非常乱而且扩展性很差

撰写答案