-2

I have two activities. I need to call a function created in activity A from Activity B. What I've done the first time is: in Activity A, I created my function:

public double [] getRSS(double [] queryC){

if (wifiList!=null){
    for (int i=0; i<wifiList.size(); i++){
    switch (wifiList.get(i).BSSID){ 
      case "56:2e:27:43:4b:f5" : queryC [0]= + wifiList.get(i).level ; break; 
      case "20:18:d8:4f:55:e8": queryC [1]  = + wifiList.get(i).level ; break; 
      case "7c:e9:d3:31:8f:b9": queryC [2] =  + wifiList.get(i).level ; break; 

    }
     }  
    }
return queryC;
}

in Activity B:

public class ActivityB extends  ActivityA{
.......
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activiteb);
.......
ActivityA w = new  ActivityA();
double [] query= w.getRSS(queryC);
.......
}
}

And then sometimes it works sometimes not! I checked for this answer Can i Create the object of a activity in other class? and I did the same thing:

static ActivityA ActivityA;

and then:

ActivityA = this; 
public static ActivityA getInstance() { return ActivityA; }

but I have error just here saying:

Syntax error on token "ActivityA", @ expected and when I changed the second class to this: ActivityA.getInstance().getRSS(queryC); I get this: The method getInstance() is undefined for the type ActivityA

Community
  • 1
  • 1
N.InFOR
  • 15
  • 5

3 Answers3

0

One activity should never reference an instance of another. Any data that needs to be passed between the two should be done via the Intent used to start the second activity, or put into singleton data structures.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • How should I do with intent? I'm taking a look at [Sending arrays with Intent.putExtra](http://stackoverflow.com/questions/3848148/sending-arrays-with-intent-putextra) is this means that I don't need to the function? can you guide me to use intent? – N.InFOR Aug 27 '16 at 13:58
  • I tried with the intent but ActicityB never start ! – N.InFOR Aug 27 '16 at 15:11
  • Help please. Thanx – N.InFOR Aug 27 '16 at 22:12
0

Don't take this path! With every activity a lifecycle is attached and creating it manually will only result in issues. Storing an instance of the activity itself just creates leaks that shouldn't happen.

Try to implement getRSS() in a class not related to an activity at all, maybe using getApplicationContext(). If necessary, store the data you need statically or hand it over to ActivityB via its intent.

Also don't use the same name as the ActivityA class for the field.

tynn
  • 38,113
  • 8
  • 108
  • 143
-2
        public class YourApplication extends Application {

            private static YourApplication mInstance;

            @Override
            public void onCreate() {
                super.onCreate();
                mInstance = this;


            public static YourApplication getInstance() {
                return mInstance;
            }

 public double [] getRSS(double [] queryC){

        if (wifiList!=null){
            for (int i=0; i<wifiList.size(); i++){
                switch (wifiList.get(i).BSSID){
                    case "56:2e:27:43:4b:f5" : queryC [0]= + wifiList.get(i).level ; break;
                    case "20:18:d8:4f:55:e8": queryC [1]  = + wifiList.get(i).level ; break;
                    case "7c:e9:d3:31:8f:b9": queryC [2] =  + wifiList.get(i).level ; break;

                }
            }
        }
        return queryC;
    }


        }

And in your ActivityA class,

YourApplication.getInstance().getRSS(queryC)

And in your ActivityB class you can access the same way.

Hope this will help you.

Thanks.

Silvans Solanki
  • 1,267
  • 1
  • 14
  • 27
  • If you create application class then you need to declared it in the manifest file as well. – Silvans Solanki Aug 27 '16 at 14:08
  • You're better off using `YourApplication.getInstance(Context context)`, which casts `context.getApplicationContext()` to the application instance and avoiding the static reference. – Passer by Aug 27 '16 at 15:43
  • It didn't solve my problem but it helped me in the way how to create the instance of the other activity – N.InFOR Aug 27 '16 at 15:58