user2430771
user2430771

Reputation: 1422

Go to home screen from Android Activity

I'm making an application in android and I want to implement a button such that whenever it is pressed, I just reach back to my home screen. I know that we have the hardware key and soft keys( when there are no hardware keys) which implements this, but I want to add this functionality for this application. Does anybody has any idea how to do it?

Thank you

Upvotes: 2

Views: 3687

Answers (3)

joao2fast4u
joao2fast4u

Reputation: 6892

The Android architecture is not ready to exit an Application with one line of code. You just cannot do it. Rely on your hardware buttons or make a finish() waterfall effect on all your Activities. You can use startActivityForResult() to start your Activities (all of them if you want this method to work). Then, when you want to exit your App, just call setResult(Activity.USER_CANCELED); and finish(); right after it. It will return to your previous Activity onActivityResult() callback. There, if the requestcode is the right one and the resultCode is equal to Activity.USER_CANCELED, just do the same: Call setResult(Activity.USER_CANCELED); and finish();. Once more, it will take you back to the previous Activity, if it exists. And so on, until you exit your app.

Upvotes: 1

Paul Boddington
Paul Boddington

Reputation: 37645

Try this

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

Upvotes: 4

nPn
nPn

Reputation: 16728

You can use an icon on your action bar and program it to bring you back to your application home screen.

Take a look at this section of the developers guide. http://developer.android.com/design/patterns/navigation.html

Upvotes: 1

Related Questions