I have two different activities. The first launches the second one. In the second activity, I call System.exit(0)
in order to force the application to close, but the first activity is automatically displayed instead of the application returning to the home screen. How can I avoid this, and get the application to return to the home screen?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Android has a mechanism in place to close an application safely per its documentation. In the last Activity that is exited (usually the main Activity that first came up when the application started) just place a couple of lines in the onDestroy() method. The call to System.runFinalizersOnExit(true) ensures that all objects will be finalized and garbage collected when the the application exits. For example:
Finally Android will not notify an application of the HOME key event, so you cannot close the application when the HOME key is pressed. Android reserves the HOME key event to itself so that a developer cannot prevent users from leaving their application.
Say you have activity stack like A>B>C>D>E. You are at activity D, and you want to close your app. This is what you wil do -
In Activity from where you want to close (Activity D)-
In your RootActivity (ie your base activity, here Activity A) -
onNewIntent is used because if activity is alive, it will get the first intent that started it. Not the new one. For more detail - Documentation
When you launch the second activity,
finish()
the first one immediately:Start the second activity with startActivityForResult and in the second activity return a value, that once in the onActivityResult method of the first activity closes the main application. I think this is the correct way Android does it.
I use this:
1) The parent activity call the secondary activity with the method "startActivityForResult"
2) In the secondary activity when is closing:
3) And in the parent activity override the method "onActivityResult":
This works fine for me.
Use the
finish
method. It is the simpler and easier method.