Lind
Lind

Reputation: 277

Starting an activity with an intent and getting the reference

I am currently using an application project in android which includes a library.

This library was an application by itself, because I want to make it integrated into the main application I added it as an external library.

Everything works fine and I use this code to start up the library application.

Intent i = new Intent(this, Launcher.class);
startActivity(i);

The problem is that I need to control this application which was added as a library. I need to disable and enable features which can only be done if I have a reference to the activity which I started.

One more thing, I want to create a menu in the primary application to control the library one. So I do not want to pass information as soon as I create it, rather do changes as the user clicks on different options.

Basically I have application A and application B which is integrated on A as a library. I want to control application B from the menu of application A after I start it normally. From let us say a menu in A with different options about B. That is why I want a reference.

My first question would be, is it possible to get a reference for the activity that I did start with the intent ?

If it is not possible, with your experience what would be the best way to achieve that, keep in mind that I cannot start implementing methods on the onCreate. The application has a clear flow, class to class and it is pretty large.

Upvotes: 3

Views: 1660

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007409

is it possible to get a reference for the activity that I did start with the intent ?

Not really.

So I do not want to pass information as soon as I create it, rather do changes as the user clicks on different options.

Well, either the second activity exists, or it does not. Usually, it does not, when the user is in your first activity (the one with the menu). In that case, you have little choice but to have the first activity tell the second activity what to do, via Intent extras, and have the second activity interpret those extras.

If the second activity already exists — and somehow you are sure that it already exists — you are welcome to use something like an event bus to have the first activity send messages that are picked up by the second activity.

In either case, you need to either modify the second activity, or subclass the second activity, to handle this inter-activity messaging.

Upvotes: 2

Related Questions