Can i Create the object of a activity in other class?

前端 未结 3 763
悲哀的现实
悲哀的现实 2020-11-22 09:34

i have defined a function in mainactivity now i want to access with another class in my app.I have created a object of the mainactivity by using that object i have called th

3条回答
  •  执念已碎
    2020-11-22 10:03

    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
    

提交回复
热议问题