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.
You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)). Remember that the name of the java file should match the class name.
We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
If you want to make Login activity your main activity then put the the intent-filter tag inside Login activity. Any activity your want to make your main activity must contain intent-filter tag with action as main and category as launcher.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With