Ask a question that does not correspond to Fragment and RadioButton.

this is the main interface, with four RadioButton. at the bottom of Fragment, at the top. When I open the main interface and select the last "my" module, then press the home key directly to return to the phone desktop. But clicking to open APP in a few minutes will be confused: RadioButton selects "my", but Fragment is indeed the first one, which should be caused by memory recycling. How to solve this problem?
Screenshot and Code:


/**
 * 
 */
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;
        }
    }
 }
Mar.20,2021

you should use a RadioGroup to manage the selection and cancellation of RadioButton, rather than click events, which is very logical and poorly scalable

.
Menu