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
I solved a similar problem: MainActivity starts BrowserActivity, and I need to close the app, when user press Back in BrowserActivity - not to return in MainActivity. So, in MainActivity:
and then, in OnResume:
... then continue OnResume. And, when start BrowserActivity:
And it looks like it works good! :-)
The easiest way for achieving this is given below (without affecting Android's native memory management. There is no process killing involved).
Launch an activity using this Intent:
In the target activity
FinActivity.class
, call finish() inonCreate
.Steps Explained:
You create an intent that erases all other activities
(FLAG_ACTIVITY_CLEAR_TOP)
and delete the current activity.The activity destroys itself. An alternative is that you can make an splash screen in finActivity. This is optional.
It is not recommended, but you can still use this. Better go with this solution in case you need to quit the app.
According to me, the best solution is to finish every activity in your app like below.
Step 1. Maintain a static variable in mainactivity. Say,
Step 2. On click event of an button, set this variable to true.
Step 3. And in every activity of your application, have the
onrestart
method as below.Try the following. It works for me.
Run the second activity using start activity for result:
Add this function to the first Activity:
And add this to the second Activity:
You can not do System.exit(), it's not safe.
You can do this one: Process.killProcess(Process.myPid());