0

I have been struggling with this problem for two days,I am in situation where i need to use a method in ActivityB from ActivityA . The problems lays in getting the context of A i have tried many solutions like:

static ActivityA activityA;

In onCreate state:

activityA = this;

and add this method:

public static ActivityA getInstance(){
   return   activityA;
}

In ActivityB, call

ActivityA.getInstance().myFunction(); //call myFunction using activityA

it did not work out because this need the ActivityA to be instantiated in order to pass its context to A but this is not accomplishable in my case is there any way of getting an activity's context without switching activities . my question might turn out to be simple or intuitive but im new to this concept , thanks in advance

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ibrahim
  • 573
  • 1
  • 6
  • 20
  • 1
    If you want to call any method from Activity A from Activity B then you must declare that method into any non Activity Class and by Passing Context and required parameter from both Activities you can use it from both Activities or simply create object of Activity A and call your method by passing Context of Activity B. – Chetan Joshi Feb 08 '17 at 10:57
  • @ChetanJoshi Can you give me a simple code example?I have tried making an interface with the method that i want to use and i implemented it in the class where i want to use it but i ended up in situation where i need to switch to the activity that contains the method in order to pass its context which i can not do in this case – ibrahim Feb 08 '17 at 11:04
  • check my answer below – Chetan Joshi Feb 08 '17 at 11:16

4 Answers4

1

As you want to have common functionality in both activities, you can create BaseActivity that extends Activity and define your method in that and extend ActivityA and ActivityB by BaseActivity then you can access methods. You can do it like this,

public class BaseActivity extends Activity
{
  public void myFunction()
 {
  ...
 }
}

And do this for other activities:

public class ActivityA extends BaseActivity
{
 public void someMethod()
 {
   myFunction();   // you can call function here directly
 }
}
0

You could extent class A using Class B simply

OR

public static ActivityA activityA;

In onCreate state:

{
activityA = this;
}

Outside Oncreate

public myFunction{
}

and in ActivityB call

activityA.myFunction();
g7pro
  • 817
  • 1
  • 6
  • 11
  • thanks for replying,since my app wont be switching to A activity (B will be opened by launch where A wont be) onCreate wont be invoked thus the context will not be passed to B,any other solutions? – ibrahim Feb 08 '17 at 11:14
  • You could extent class A using Class B simply – g7pro Feb 08 '17 at 11:26
0

Here I Created Two Classes Consider as Activities , And Then Created one Public methodA() in class Activity_A , then Created Class Activity_B and Created methodB() , And Created Object of Activity_A and Called methodA() by passing context of Activity Activity_B .

        class Activity_A{
            public void methodA(Context context){
                Toast.makeText(context,"methodA",Toast.LENGTH_LONG).show();
            }
        }

        class Activity_B{
            public void methodB(){
                Activity_A activity_a = new Activity_A();
                activity_a.methodA(Activity_B.this);
            }
        }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • thanks for replying , unfortunately this gave me a NullPointerException on this line: activity_a.methodA(Activity_B.this); – ibrahim Feb 08 '17 at 11:40
  • If you are using any view or sub methods from method of Activity_A which uses Context of Activity_A then it may happens .So make sure your calling method does not update any UI component (views ) of Activity_A .Bcoz now you have passes Context Of Current Actvity_B. – Chetan Joshi Feb 08 '17 at 11:45
  • Add your method in your Question , So its clear , What are you want to do. – Chetan Joshi Feb 08 '17 at 11:45
  • my code have gone into a real mess it would look intimidating to post it here since its too nested ,basically i have a method in activity A which deletes saved files and the other activity (B activity ) has a listview displaying all the saved text files(they are notes actually) so when i long press the notes in B activity i get delete option and im trying to call the delete method from A when the user choose to press the delete icon , i did achieve this by inheritance as others suggested which is great in this case, but still wondering how to get a context without having to open the activity – ibrahim Feb 08 '17 at 12:03
0

There are two options:

1) Add the static keyword to your shared methods

OR

2) You can try reflection.

For reference follow the link: What is reflection and why is it useful?

Community
  • 1
  • 1
BhanuSingh
  • 118
  • 1
  • 2
  • 15