-1

I am creating a class in application with following code:

public class Trackers extends Application
{
       private static Connection connection;
       public Connection getConnectionInstance() {
           if (connection == null)
               connection = new Connection();
           return connection;
       }
}

I need to pass Main activity to Connection class.Is this possible ? If it is possible,how ?

Okan
  • 1,379
  • 3
  • 25
  • 38

1 Answers1

1

You can put your main activity as a parameter to connection constructor:

public class Trackers extends Application
{
       private static Connection connection;

       public Connection getConnectionInstance(MainActivity main) {
           if (connection == null){
               connection = new Connection(main);
           }
           return connection;
       }
}

of course, I assume that if you want to pass the activity to Connction, connection needs to support getting the main activity

if you want to call method from activty, look here: How to call a method in activity from a service

or here: android start activity from service

Community
  • 1
  • 1
yossico
  • 3,421
  • 5
  • 41
  • 76