简体   繁体   English

app内的主页按钮

[英]Home button within app

I want to create a "home" button that will bring the user back to the "Home" in my app from whatever activity they are in at the time. 我想创建一个“主页”按钮,将用户从当时的任何活动中带回我应用程序中的“主页”。 I have overridden the action bar so I won't be able to use one. 我已经覆盖了操作栏,所以我将无法使用它。 My problem is that when 我的问题是什么时候

Home → A → B

I want to press the home button to return to Home. 我想按主页按钮返回主页。 Originally I had the home button simply finish() the activity but in this case that will return me to A. Is there a way to do what I'm attempting? 最初我有主页按钮只是finish()活动,但在这种情况下,我将返回到A.有没有办法做我正在尝试的?

Just to clarify this is an ImageButton on screen, not the hardware home button on the device. 只是为了澄清这是屏幕上的ImageButton ,而不是设备上的硬件主页按钮。

You can use the Intent-Flag FLAG_ACTIVITY_CLEAR_TOP 您可以使用Intent-Flag FLAG_ACTIVITY_CLEAR_TOP

If you don't have many Activities then you can simply create an onClick in each that is something like 如果你没有很多Activities那么你可以简单地在每个Activities创建一个onClick

Intent intent = new Intent(CurrentActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

this flag will take you back to the "Home" Activity and clear all others off of the stack 此标志将带您回到“Home” Activity并清除堆栈中的所有其他Activity

If you have many Activities then it may be easier to create a base Activity from which to extend all others and include the home button functionality inside that base Activity 如果您有许多Activities那么创建一个基本Activity可能会更容易,从中扩展所有其他活动并在该基础Activity包含主页按钮功能

You can simply have your Image Button create an intent to start your home screen activity, assuming this is what you were looking for. 您可以简单地让您的图像按钮创建一个开始主屏幕活动的意图,假设这是您正在寻找的。 within the OnClick of the button, generate an intent, and startActivity(intent) 在按钮的OnClick中,生成一个intent和startActivity(intent)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM