This seems like it should be easy, so I must be missing something obvious: I have 4 standalone applications in the same package, us.glenedwards.myPackage,
- myClass1 extends Application
- myClass2 extends Application
etc...
I need each class to act as its own standalone application. Yet I want to be able to start the other 3 classes from the one I'm in by clicking a link. Android allows me to do this using Intents:
Intent intent = new Intent(this, EditData.class);
overridePendingTransition(R.layout.edit_data_scrollview, R.layout.state);
startActivity(intent);
I've tried starting myClass2 from myClass1 using
myClass2.launch("");
But I get an error, "Application launch must not be called more than once". The only way I can get it to work is if I remove both "extends application" and the start() method from myClass2, which means that myClass2 is no longer a standalone application.
How can I start myClass2, myClass3, or myClass4 from myClass1 with all 4 of them being standalone applications?
have you tried this?
Also, handle/catch the exceptions, as needed.
I was right; it was a no-brainer. That's what I get for writing code on 4 hours of sleep:
You can make this work by calling
start(...)
directly on a new instance of one of theApplication
subclasses, but it kind of feels like a bit of a hack, and is contrary to the intended use of thestart(...)
method. (Just semantically: a method calledstart
in a class calledApplication
should be executed when your application starts, not at some arbitrary point after it is already running.)You should really think of the
start
method as the replacement for themain
method in a traditional Java application. If you had one application calling another application'smain
method, you would (hopefully) come to the conclusion that you had structured things incorrectly.So I would recommend refactoring your design so that your individual components are not application subclasses, but just plain old regular classes:
and, similarly,
Now you can just do things like
anywhere you need to do them. So you can create standalone applications for each module:
or you can instantiate them as part of a bigger application:
and
The way I think of this is that
Application
subclasses represent an entire running application. Consequently it makes sense only to ever instantiate one such class once per JVM, so you should consider these inherently not to be reusable. Move any code you want to reuse into a different class somewhere.