I want to know if user would return to the home screen if he exit the current activity.
相关问题
- 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'm going to post the comment of @H9kDroid as the best answer here for people that have a similar question.
You can use isTaskRoot() to know whether the activity is the root of a task.
I hope this helps
I've created a base class for all my activities, extending the AppCompatActivity, and which has a static counter:
This has worked well enough, so far; and regardless of API version. It requires of course that all activities extends this base class - which they do, in my case.
Edit: I've noticed one instance when the counter goes down to zero before the app completely exits, which is when the orientation is changed and only one activity is open. When the orientation changes, the activity is closed and another is created, so
onDestroyed
is called for the last activity, and thenonCreate
is called when the same activity is created with the changed orientation. This behaviour must be accounted for; OrientationEventListener could possibly be used.One way to keep track of this is to include a marker when you start a new activity and check if the marker exists.
Whenever you start a new activity, insert the marker:
And you can then check for it like this:
Android implements an Activity stack, I suggest you read about it here. It looks like all you want to do though is retrieve the calling activity:
getCallingActivity()
. If the current activity is the first activity in your application and the application was launched from the home screen it should (I assume) returnnull
.The Problem with sandrstar's solution using
ActivityManager
is: you need a permission to get the tasks this way. I found a better way:The
Activity
on the Stack bottom should allways get this category by default while other Activities should not get it.But even if this fails on some devices you can set it while starting your
Activity
:The one thing that missed here, is the "Home key" click, when activated, you can't detect this from your activity, so it would better to control activity stack programmatically with handling "Back key" press and moving to required activity or just doing necessary steps.
In addition, you can't be sure, that starting your activity from "Recent Activity" list can be detected with presetting some extra data into intent for opening activity, as it being reused in that case.