I have two activites called Activity_A
and Activity_B
. I have a Method or Function in Activity_A like:
public void printNumber (int i) {
for(int j = 0; j <= i; j++) {
Log.w("TAG", "Print number is: " + j);
}
}
Now I want to call this Method from my another activity called Activity_B.
I am trying to call this Method using following lines:
((Activity_A) this.getApplicationContext()).printNumber();
I write this line in onCreate of Activity_B and this will crash my application and logcat shows this error:
java.lang.ClassCastException: android.app.Application cannot be cast to com.example.app.Activity_A
How can I do this?
Edit: I found this question and acording to "Rich" extends Activity_A
to Activity_B
. But the problem is Activity_B
is a list activity and I have already extends this with ListActivity
.
and other answer create an instance of Activity_A
and then call method. If I do this a all the variable's become empty or null of Activity_A
. And I don't want to make a static method.
Is there any other way to do this without create a static reference or another instance?