1

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?

Just to clarify this is an ImageButton on screen, not the hardware home button on the device.

4

2 回答 2

3

您可以使用Intent-Flag FLAG_ACTIVITY_CLEAR_TOP

如果你没有很多Activities,那么你可以简单地在每个中创建一个onClick类似于

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

此标志将带您回到“主页”Activity并清除堆栈中的所有其他标志

如果你有很多Activities,那么创建一个基础可能会更容易Activity,以扩展所有其他基础并在该基础中包含主页按钮功能Activity

于 2013-07-22T17:52:43.773 回答
0

您可以简单地让您的图像按钮创建一个意图来启动您的主屏幕活动,假设这是您正在寻找的内容。在按钮的OnClick内,生成一个intent,然后startActivity(intent)

于 2013-07-22T17:55:57.110 回答