2

Unable to access the methods from one class to another class in android. Actually, what I am trying is

ClassA.java

class ClassA extends Activity
{
    method_1();
}

ClassB.java

class  ClassB extends BroadCastReciever
{
    // I need to access the method_1 from the class ClassA.
}

How can I do this?

midhunhk
  • 5,560
  • 7
  • 52
  • 83
Nakshatran
  • 426
  • 1
  • 7
  • 19

7 Answers7

1

inside ClassA declare public method from ClassB create object of ClassA and access method using object write below code inside ClassB

Like

ClassA  classa = new ClassA();
classa.method_1(); 

Note :

  • if possible don't create class as static if it is not necessary.

  • provide class modifier to public.

Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
1

First Solution

Declare your method1() as follows -

public static void method_1()
{

} 

Then you can access it from ClassB as follows -

ClassA.method1();

Second Solution

Create an object of ClassA and access method1() from ClassB as follows-

ClassA classA = new ClassA();
classA.method1();

EDIT:

If both the classes are in the same package then you can use protected instead of public in the First Solution.

user1721904
  • 843
  • 4
  • 13
0

there is two way to access it

1) you need to create method as public static method() so that you can access it anywhere by calling class name dot method name like Aaa.method();

2)or you need a object to call method like passing Aaa's object to broadcast class and call method on it.

EDIT:

if you are passing context from activity A to brodcast class then you can convert context into activity object to call its public methods.

like: in brodcast class:

((yourActivityName)context).method();
NaserShaikh
  • 1,576
  • 2
  • 23
  • 39
0

mark method_1(); as public and static Eg:

class ClassA extends Activity
{
    public void static method_1();
}

Then you should be able to access method_1(); without creating a object of class A.

0

i guess you cant access a method from a class that extends an activity in android , however you can add an inner or helper class to the activity class like this example :

Call a public method in the Activity class from another class?

Community
  • 1
  • 1
mmoghrabi
  • 1,233
  • 1
  • 14
  • 23
0

Basically you can call any method from another class, as long as it's public. If the method is static, you don't even need to instantiate it. But read some tutorials to get the basic idea of OOP instead of just looking for a solution for your specific problem, it'll help a lot more!

pooja
  • 107
  • 1
  • 2
  • 7
0

You should never be able to to that ... How do you know if the activity is still alive? The best way to send messages from BroadcastReceivers to Activities (where you register a custom receiver in onCreate/onresume and unregister in onDestroy/onPause) is to send another broadcast message from BroadcastReceiver.

But you could catch the initial broadcast already in activity. Consider using OrderedBroadcastReceivers.

gunar
  • 14,660
  • 7
  • 56
  • 87