سؤال

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.

هل كانت مفيدة؟

المحلول

You can use the 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

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

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

نصائح أخرى

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)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top