Yair Landmann
Yair Landmann

Reputation: 117

How to get which activity is running android studio

I`m trying to check the flow of my program and to do so I need a way to get the type of a class that is currently running.

I tried searching the web for some info about the subject but as I see most of the examples add a static variable for the active data.

Beside testing the logic of my program I want to test the GUI as well so I will be able to check that after I press a button the correct activity is running.

There isn't much info about testing on Android studio so any help will be highly appreciated (Such as "You are in a completely wrong path, you can test your GUI by...")

Thanks

Upvotes: 2

Views: 766

Answers (1)

Murat Güç
Murat Güç

Reputation: 377

You can use Application.ActivityLifecycleCallbacks interface for this approach.

First step is implement Application.ActivityLifecycleCallbacks to your Application class.

class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {...}

Second step is just register for Application.ActivityLifecycleCallbacks callbacks in your MyApplication.class's onCreate method

 @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
    }

Last step is just override the methods from Application.ActivityLifecycleCallbacks in MyApplication class

void onActivityCreated(Activity var1, Bundle var2);

void onActivityStarted(Activity var1);

void onActivityResumed(Activity var1);

void onActivityPaused(Activity var1);

void onActivityStopped(Activity var1);

void onActivitySaveInstanceState(Activity var1, Bundle var2);

void onActivityDestroyed(Activity var1);

Now you can easily know which activity is currently running :) Happy coding!

Upvotes: 1

Related Questions