Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current on screen activity? [duplicate]

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?

like image 472
Gerhard Avatar asked Aug 08 '11 18:08

Gerhard


People also ask

How can I get 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.

How can I get running activity in Android?

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.

How do you know if activity is visible or not?

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.


1 Answers

Sometimes Fragments need to know which Activity they belong to. For this, use Fragment.getActivity(). This works since API 11.Refer this

like image 179
Sparky Avatar answered Oct 18 '22 05:10

Sparky