0

现在大致的流程见图,想实现功能的是:不管开启多少个Fragment,回到主界面后点击返回都是直接退出APP,返回到手机桌面.
尝试过两种方法不行,
1.在图片中新的Activity点击完成后清空所有Fragment回退栈

FragmentManager fragmentManager = getFragmentManager();
int count = fragmentManager.getBackStackEntryCount();
for (int i = 0; i < count; ++i) {
    fragmentManager.popBackStack();
}

2.在主界面的onBackPress里面判断当前是否处于主界面,如果是处于主界面,就清空所有Activity,但是还没有找到怎样判断是当前主界面的方法,因为我通过Fragment一开启了Fragment二,当前也还是处于主界面;底部"首页"和"我的"模块还要存在的;
图片描述

3.MainActivity使用singleTop或singleTask启动模式也无效,代码:

public class MainActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_shoot)
    ImageView mShoot;
    @BindView(R.id.vp_container)
    NoScrollViewPager mViewPager;
    @BindView(R.id.ctl_bottom_bar)
    CommonTabLayout mCommonTabLayout;

    private static final String TAG = "MainActivity";
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles = {"工具", "用户"};     /*底部导航标题*/
    private int[] mIconUncheckIds = {R.mipmap.tool_unselect, R.mipmap.user_unselect};       /*未选中的icon*/
    private int[] mIconCheckedIds = {R.mipmap.tool_selected, R.mipmap.user_selected};   /*选中时的icon*/
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private VersionPresenter presenter = new VersionPresenter(this);
    long waitTime = 2000;
    long touchTime = 0;
    ToolFragment toolFragment;

    public static void open(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected View initView(Bundle savedInstanceState) {
        View view = View.inflate(this, R.layout.activity_main, null);
        ButterKnife.bind(this, view);
        frameLayout.addView(view);
        mToolbar.setVisibility(View.GONE);
        String uid = UserManager.getInstance().getUid();
        Log.d(TAG, "initView: 1026=  " + uid);

        return view;
    }

    @Override
    protected void initData() {
        PermissionUtil.getPermission(this);    //获取动态存储权限
        toolFragment = ToolFragment.getInstance();
        mFragments.add(toolFragment);
//        mFragments.add(ToolFragment.getInstance());
        mFragments.add(UserFragment.getInstance());
        /*添加数据集*/
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconCheckedIds[i], mIconUncheckIds[i]));
        }
        mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager(), mFragments, mTitles));
        setLayout();
        //设置未读消息红点
//        mCommonTabLayout.showDot(1);
        presenter.checkVersion(false);//检查版本号
    }

    @Override
    protected void initEvent() {
        mShoot.setOnClickListener(this);
    }

    private void setLayout() {
        mCommonTabLayout.setTabData(mTabEntities);
        /*选择事件*/
        mCommonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position, false);  //false取消切换过渡动画
//                if (position == 1) {
//                    Toast.makeText(MainActivity.this, "选中了" + position, Toast.LENGTH_SHORT).show();
//                }
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mCommonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mViewPager.setCurrentItem(0);
    }

    @Override
    public void onClick(View v) {
        RecordActivity.open(this);
//        TakePictureActivity.open(this);
    }

    @Override
    public void onBackPressed() {
        //TODO:判断当前是否是主界面,主界面退出应用

        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        int size = fragments.size();
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fra = fragments.get(i);
//            Log.d(TAG, "onBackPressed: 1743" + fra);
        }
//        boolean visible = ToolFragment.getInstance().isVisible();
//        boolean visible = toolFragment.isResumed();

        View view = toolFragment.getView();
        ImageView toolRecyclerView = view.findViewById(R.id.iv_task);
       boolean isVisible= toolRecyclerView.getVisibility()==View.VISIBLE;
//        toolRecyclerView.getLocalVisibleRect();

        Log.d(TAG, "onBackPressed: 1743    = "+isVisible);

        super.onBackPressed();
    }

}
2019-04-17 提问
2 个回答
0

用这个方法

MainActivity

   if(getFragmentManager().getBackStackEntryCount() > 0) {
         getFragmentManager().popBackStack();
      }else{
         super.onBackPressed();
     }
0

如果你首页是tab形式内嵌Fragment展示各个界面的,你可以用viewpager那种来实现;
或者
getSupportFragmentManager().beginTransaction()

                    .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                    .commit();

不要每次add,采用replace

撰写答案

推广链接