6

I have defined a function in MainActivity now I want to access the function from another class in my app. I have created an object of the MainActivity and with that object I have called the function. Although there is no error, it's not executing. Every time I try to execute, the app crashes.

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36

3 Answers3

34

Activity A should have a variable

static ActivityA activityA;

In onCreate state:

activityA = this;

and add this method:

public static ActivityA getInstance(){
   return   activityA;
 }

In activity B, call

ActivityA.getInstance().myFunction(); //call myFunction using activityA
Aduait Pokhriyal
  • 1,529
  • 14
  • 30
Lumis
  • 21,517
  • 8
  • 63
  • 67
  • +1 for answer...This is what I have discussed with Raghav Sood the answer belwo his comments – Pragnani Feb 19 '13 at 11:47
  • 2
    Yes, this is only needed if a top activity should control the activity below it. e.g. one can use a transparent activity as a graphic interface to control an activity below it. Otherwise it is better that the function is a static one in a separate utilities class and pass the context if required. – Lumis Feb 19 '13 at 12:06
  • its called methods but can't change setbackgroundresources().... of imageView .... – Buggy IdioT Sep 30 '14 at 06:28
  • Didn't get the `myFunction` – Srujan Barai Aug 29 '15 at 19:21
  • Thank you so much it is working fine. Creating any activity's object using the code `MyActivity activity = new MyActivity();` will not go through any life cycle of activity and will generate null pointer exception. – Prithniraj Nicyone Mar 11 '16 at 08:03
  • @Lumis Will this not lead to memory leak? If Android decides to destroy activity under memory pressure, you are holding reference to Activity which is destroyed !!! – Akash Kava Oct 10 '16 at 13:41
  • This will cause memory leak, instead you could create an interface and implement inside your activity – Visal Varghese Feb 19 '17 at 06:34
  • 1
    Using this approach how will you give the guarantee that the static activity object gets garbage collected when the system needs it to. – Paramvir Singh Nov 12 '17 at 02:26
  • This could cause memory leaks. right? – Rohit Singh Feb 11 '22 at 16:39
18

You cannot just create objects of Activities by using:

MyActivity activity = new MyActivity();

as you would with normal Java classes. All Activities in Android must go through the Activity lifecycle so that they have a valid context attached to them.

By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.

Instead, move all such methods which need to be called from other classes into a Utility class which accepts a valid context in its constructor, and then use that context in the methods to do the work.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • I know about using generic util class for the accessing the common methods...But What about taking a static method in the activity and calling it by using the class name...? Is it wrong? what Happens if we do like so.. – Pragnani Feb 19 '13 at 11:22
  • 1
    @Pragnani Static methods cannot refer to non static members of a class. As the class context is not static, you will still get an error. – Raghav Sood Feb 19 '13 at 11:23
  • ,yeah I know that..but How can we access the static method of Calender class i.e Calender.getInstance() in the our non static class..eventhough it is factory method but it is static – Pragnani Feb 19 '13 at 11:27
  • Calendar.getInstance() is used to return an Instance of the calendar class based on your locale etc, which you use to call other methods on the calendar class. – Raghav Sood Feb 19 '13 at 11:30
  • Sorry I know the usage of the Calender.getInstance() method, in the previous comment You've said that we can't use static methods in the non static members of a class, but You missed something that "If can not refer static members in the not static content" but we can use the static method in our class – Pragnani Feb 19 '13 at 11:43
  • that's why I have mention getInstance() method, getInstance() method is a static, How do we use it in our class to get the calander instance..What about Thread.CurrentThread() – Pragnani Feb 19 '13 at 11:44
  • You can call static methods from a non static method, but you cannot use a non static variable or method inside a static method. – Raghav Sood Feb 19 '13 at 11:46
  • 1
    @Pragnani Yes. But you cannot make a static method in your Activity that calls anything using the Activity instance or context and then call it in another class – Raghav Sood Feb 19 '13 at 11:49
0

Make the variable public and then create object in adapter like this:

public int i;  // Variable in Activity class

((ActivityName) context).i          // accessing in adapter 
Adrian W
  • 4,563
  • 11
  • 38
  • 52
Sumit Singh
  • 1,150
  • 13
  • 17