2

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

user2430771
  • 1,326
  • 4
  • 17
  • 33

3 Answers3

4

Try this

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
1

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

nPn
  • 16,254
  • 9
  • 35
  • 58
  • Hi. Thanks for your reply. I don't want to go to first activity of my application. I want to exit my application and reach to phone's home screen. Any idea how to implement this. – user2430771 Sep 09 '14 at 22:36
  • In that case you would call finish(); on each activity. – nPn Sep 09 '14 at 22:40
  • but I am not sure why you would just not rely on the hardware or soft home button? – nPn Sep 09 '14 at 22:43
  • If I do that I think I will reach to menu page which will have list of applications but not the home screen. I'm making an application which can have it's own navigation buttons and disabling the default navigation buttons provided by Android. I have disabled application button but do not know the methods which can implement behavior of default navigation button. – user2430771 Sep 09 '14 at 22:54
1

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.

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42