gpt4 book ai didi

android - onBackPressed() 应用程序未关闭

转载 作者:行者123 更新时间:2023-11-29 23:27:06 25 4
gpt4 key购买 nike

我有 3 个 Activity 第一个是登录第二是注册第三个主页 Activity (Bottom_nav) 带有底部导航,有 3 个 fragment A、B、C。

登录 Activity 是启动器 Activity 。

检查登录用户是否已经登录如果用户未登录,则重定向到注册 Activity 除了家庭 Activity 。

我在家庭 Activity 中实现了 onBackPressed()但是双击后退按钮 Activity 会暂停并恢复。

这是我的代码:

 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

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

最佳答案

请使用这个。

boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (!doubleBackToExitPressedOnce){
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);


}else {
if (getSupportFragmentManager().getBackStackEntryCount() != 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
finish();
}

}
}

在打开 fragment 时你必须使用这个

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

关于android - onBackPressed() 应用程序未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53368374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com