简体   繁体   English

“后退”按钮转到父活动,而不是转到电话主屏幕

[英]Back button goes to the parent activity instead going to the phone home screen

I have two activities, A y B. A is the parent activity of B. 我有两个活动,A yB。A是B的父活动。

A initialise the second one with this code: 使用以下代码初始化第二个:

Intent intent = new Intent( this, B.class );
startActivity( intent );
this.finish();

and the B goes back to A (like a logout) with this code: B使用以下代码返回A(如注销):

Intent intent = new Intent( B.this, A.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity( intent );

Giving you some more context, the app has a sliding menu and each list item shows a ListFragment within a ListView. 给您更多上下文,该应用程序具有一个滑动菜单,每个列表项在ListView中显示一个ListFragment。 On each of those views the back stack has been cleaned and the idea is when the user press onto the back button, he should go to the phone home screen as Google documentation says and when the user press on the app icon from the phone home screen, it needs to re-init where the user was working on. 在这些视图中的每个视图上,都清洁了后盖,其想法是用户按下后退按钮时,应按Google文档所述进入电话主屏幕,以及当用户从电话主屏幕按应用程序图标时,它需要重新初始化用户的工作位置。

What the app is currently doing is going to the phone home screen, but when you press onto the app icon, it starts again from the activity A, which is the launch and main activity. 该应用程序当前正在执行的操作将进入手机主屏幕,但是当您按一下该应用程序图标时,它将再次从活动A(启动和主要活动)开始。

Any idea why? 知道为什么吗?

Try adding after startActivity() finish() . 尝试在startActivity() finish()之后添加。 That will make the provius activity to safely close itself. 这将使provius活动能够安全地自行关闭。

You use this code to go to phone Home screen 您使用此代码转到手机主屏幕

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

Override onBackPressed and write the above code. 覆盖onBackPressed并编写上面的代码。 It should work. 它应该工作。

According to my understanding, You have 2 activities A & B. 据我了解,您有2个活动A和B。

You are starting Activity A & moving to Activity B same timing calling A.this.finish() 您正在启动活动A并移至活动B,同时调用A.this.finish()

It means there is only 1 activity present in your stack which is Activity B. 这意味着堆栈中只有1个活动,即活动B。

And when you press "Back" button, it means its closing your app that removing remaining Activity B from stack, so your stack gets empty. 而且,当您按下“返回”按钮时,这意味着它关闭了您的应用程序,从而从堆栈中删除了剩余的Activity B,因此您的堆栈为空。 So you need to press "Center" button of your device which will keep your app activity B in background (in onStop() state) & when you start your app again so it will open activity B only (by calling onRestart()). 因此,您需要按下设备的“居中”按钮,这将使应用程序活动B处于后台(处于onStop()状态),而当您再次启动应用程序时,它将仅打开活动B(通过调用onRestart())。

I hope above explanation is highly sufficient to understand above problem. 我希望以上解释足以理解以上问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 片段中的“后退”按钮返回到主屏幕 - Back button in fragment goes back to Home screen 后退按钮将我带到主屏幕,而不是以前的活动 - Back button takes me to home screen instead of previous activity 单击“后退”或“主页”按钮时,应用程序将返回活动的父级,但并不总是调用onStart() - When clicking back or home button, app goes back to activity's parent but onStart() isn't always called 从Activity而不是父级返回主屏幕 - Returning to home screen from Activity instead of parent 启动器开发 - 主页按钮不会回到初始活动 - Launcher development - Home button not going back to initial activity 按下应用程序图标按钮返回父活动 - going back to the parent activity on pressing app icon button 后退按钮退出应用程序而不是转到上一个活动 - back button exits app instead of going to previous activity 后退按钮不会带我去之前的活动,而是主屏幕 - back button not taking me to previous activity, rather home screen 从设置中按返回按钮,它将返回主屏幕,而不是当前活动 - onPress of back button from settings it returns to home screen not the current activity 后退按钮正在关闭应用程序而不是回到父活动 - Back button is closing application instead of getting back to parent Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM