Possible Duplicate:
Android: How can I get the current foreground activity (from a service)?
In a method I have to know which activity is on the screen. How do I get the current activity?
This example demonstrates How to get current activity name in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.
There's an easy way of getting a list of running tasks from the ActivityManager service. You can request a maximum number of tasks running on the phone, and by default, the currently active task is returned first. Once you have that you can get a ComponentName object by requesting the topActivity from your list.
In your finish() method, you want to use isActivityVisible() to check if the activity is visible or not. There you can also check if the user has selected an option or not. Continue when both conditions are met.
Sometimes Fragments need to know which Activity they belong to. For this, use Fragment.getActivity(). This works since API 11.Refer this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With