我有3个活动
第一个是登录
第二是登记
第三次家庭活动(Bottom_nav),具有底部导航功能,并包含3个片段A,B,C。

登录活动是启动器活动。

登录用户已登录或未登录
如果用户未登录,则重定向到注册活动
否则要进行家庭活动。

我已经在家庭活动中实现了onBackPressed()
但是在双击返回按钮活动时,暂停并恢复。

这是我的代码:

 boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        Bottom_nav.this.finish();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;
        }
    }, 2000);
}




private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                getSupportFragmentManager().beginTransaction().replace(R.id.MainFrame, new HomeFragment()).commit();
                return true;
            case R.id.navigation_dashboard:
                getSupportFragmentManager().beginTransaction().replace(R.id.MainFrame, new ProfileFragment()).addToBackStack(null).commit();
                return true;
            case R.id.navigation_notifications:
                getSupportFragmentManager().beginTransaction().replace(R.id.MainFrame, new NotificationFragment()).addToBackStack(null).commit();
                return true;
        }
        return false;
    }
};

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

    getSupportFragmentManager().beginTransaction().add(R.id.MainFrame, new HomeFragment()).commit();
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

}


这是日志

V/FA: Recording user engagement, ms: 2650
V/FA: Activity paused, time: 132340044
D/FA: Logging event (FE): user_engagement(_e),
Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=2650,
firebase_screen_class(_sc)=Bottom_nav,
firebase_screen_id(_si)=-4687850249260059972}]
D/FA: Logging event (FE): screen_view(_vs),
Bundle[{firebase_event_origin(_o)=auto,
firebase_previous_class(_pc)=Bottom_nav,
firebase_previous_id(_pi)=-4687850249260059972,
firebase_screen_class(_sc)=RegisterPhoneActivity,
firebase_screen_id(_si)=-4687850249260059974}]
V/FA: onActivityCreated
V/FA: Activity resumed, time: 132340054
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 184
V/FA: Activity paused, time: 132340135
D/FA: Logging event (FE): screen_view(_vs),
Bundle[{firebase_event_origin(_o)=auto,
firebase_previous_class(_pc)=RegisterPhoneActivity,
firebase_previous_id(_pi)=-4687850249260059974,
firebase_screen_class(_sc)=Bottom_nav,
firebase_screen_id(_si)=-4687850249260059971}]
V/FA: Activity resumed, time: 132340244
W/PathParser: Points are too far apart 4.000000000000002
Points are too far apart 4.000000000000002
W/PathParser: Points are too far apart 4.000002058290173
Points are too far apart 4.000002058290173
W/PathParser: Points are too far apart 4.000001624254724
W/PathParser: Points are too far apart 4.000002923700373
Points are too far apart 4.000002923700373
W/PathParser: Points are too far apart 4.00000093640932
V/FA: Inactivity, disconnecting from the service


我要去哪里错了?我没有找到任何与此相关的答案

最佳答案

如果启动器屏幕为登录状态,则使用finishAffinity()方法将完成当前活动和所有父活动,但仅在Android 4.1或更高版本中有效。

if (doubleBackToExitPressedOnce) {
    super.onBackPressed();
    Bottom_nav.this.finishAffinity();
    return;
}


如果您不想在片段中回溯,请使用它。

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();


或者你可以改变自己的方式

    getSupportFragmentManager().beginTransaction().replace(R.id.MainFrame, new HomeFragment()).addToBackStack(null).commit();

10-07 19:33