0

So I'm working with Java in android studio and I want to start a new class from a different class (I'm in ListenerServiceFromWear and want to start MainActivity) and once Mainactivity is started I want to start a method (startEmergencyMode();) in Mainactivity.

How do I do this from ListenerServiceFromWear?

litelite
  • 2,857
  • 4
  • 23
  • 33
hxwtch
  • 135
  • 3
  • 14
  • ``MainActivity ma = new MainActivity(); ma.startEmergencyMode();`` ? – f1sh May 18 '17 at 14:56
  • I tried that but it gives an error with view.findById() because it's looking in the listener.java file when It's supposed to be looking in MainActivity. That's why I thought that I was maybe calling a method from another java file instead of actually starting the mainactivity file and then call the method – hxwtch May 18 '17 at 15:02

2 Answers2

1

Start MainActivity with an intent and in the extra of the intent put some flag that will tell MainActivity to call startMergencyMode()

Intent intent = new Intent(this, Mainactivity.class);
intent.putExtra("isEmergency", true);
startActivity(intent);

And then in Mainactivity actually call startEmergencyMode()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...

    Intent intent = getIntent();
    boolean isEmergency = intent.getBooleanExtra("isEmergency", false);

    if(isEmergency){
        startEmergencyMode();
    }
}
litelite
  • 2,857
  • 4
  • 23
  • 33
  • thanks. But by starting the intent I get the error: android.app.Application cannot be cast to com.my_emergency.samdesmedt.my_emergency.MainActivity – hxwtch May 18 '17 at 15:29
  • @hxwtch It may the same problem as [this](http://stackoverflow.com/questions/18830205/application-crash-with-android-app-application-cannot-be-cast-to) – litelite May 18 '17 at 15:35
0

I don't quite understand what you mean by "start"

In java, you either:

  • Declare a static field or method
  • Create an instance of an object and use its public fields and methods.

If you wish to just have one 'instance' of MainActivity, use a static method:

public static void startEmergencyMode() {
    // Code here
}

Which you can call anywhere using MainActivity.startEmergencyMode(). Keep in mind that this static method can only access static fields and other static methods.

If you wish to create an instance of MainActivity, simply create one and call the method:

public void startEmergencyMode() {
    // Code here
}


// Somewhere else
MainActivity activity = new MainActivity();
activity.startEmergencyMode();

If you don't understand the difference between a static and non static method or field, refer the answer on this thread: What does 'public static void' mean in Java?

Community
  • 1
  • 1
086
  • 197
  • 13
  • This works but if I do this it doesn't find the view elements because I'm working in a different java class. I thought I'd maybe have to start the activity first with an intent and then call the method? – hxwtch May 18 '17 at 15:04
  • I'm not an adroid developer but I'm guessing `ListenerServiceFromWear` is a class you made? If so, from where? If it's made from `MainActivity`, just pass the instance of `MainActivity` in the constructor of `ListenerServiceFromWear` using `this`, and then use that object to call the method on – 086 May 18 '17 at 15:07
  • If you do it like that, the activity will not be in android's loop. So the lifecycle events will not be called. – litelite May 18 '17 at 15:16