0

I want to ask if it is possible to start one non-activity Class from another non-activity Class

I.e:

public class nubmerOne {
    String data = "send me please";
    public String getInfo(){

        if (/*something happens*/){
          //start Class numberTwo with some data
        }
    return "";
    }
}


public class numberTwo { //this class is started when condition in class One is true
String dataPlus = "";
    public String dosomething(){
       //started from class numberOne and get data

    dataPlus = data + "";  
    return dataPlus; 
    }
}

I know this is possible between BroadcastReceiver, Class that extends Activity and Service, bud it is possible to do it within two classes ?

2
  • What do you mean with starting class? There is a definition for Activity and Service but that is specific for them and won't work for other classes.
    – StenSoft
    Feb 11, 2015 at 23:57
  • I ment to start all methods in class numberTwo, but its solved now thanks :)
    – RobertS
    Feb 12, 2015 at 0:09

1 Answer 1

0

I'm not sure I understand your question, but this looks like just classic Java.

Your first class must create the instance of the second class if the condition is satisfied and pass the data in the constructor of the second class.

public class nubmerOne {
    String data = "send me please";
    public String getInfo(){

        if (/*something happens*/){
          //start Class numberTwo with some data
          NumberTwo numberTwo = new NumberTwo (data);
          numberTwo.doSomething();
        }
    return "";
    }
}


public class NumberTwo { //this class is started when condition in class One is true
   private String data;

   public NumberTwo (String data){
      this.data = data;
   }
String dataPlus = "";
    public String dosomething(){
       //started from class numberOne and get data

    dataPlus = data + "";  
    return dataPlus; 
    }
}

Hope I helped.

1
  • Thanks works fine, I tried to do it with broadcast, but this is better.
    – RobertS
    Feb 12, 2015 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.