简体   繁体   中英

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

Try this

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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