3

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.

Lind
  • 277
  • 2
  • 5
  • 16
  • iirc you can do something like: `Intent callingIntent = getIntent()` in your activity that was opened (Activity B) and you can run something like `String activityName = callingIntent.getSimpleClassName()` to get the classname of the activity that opened (Acitivity A) Activity B – Razgriz Apr 23 '16 at 12:42
  • @Razgriz Yeah but what good would that do me ? I want to have a reference to the Activity B was the opened by activity A. The one which I want to control is Activity B, from the Application A let us say. – Lind Apr 23 '16 at 12:45
  • Editing the question to make it more clear – Lind Apr 23 '16 at 12:45
  • Have you checked the following : http://stackoverflow.com/questions/19026515/how-to-use-interface-to-communicate-between-two-activities ? – Rany Albeg Wein Apr 23 '16 at 12:48
  • @RanyAlbegWein I just did, the problem is that I do not want a reference at B from A. I want a reference to A for B. – Lind Apr 23 '16 at 12:51

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I think it counts as it exists, since the second activity is a launcher. I want the first activity to control the second one through functions which the second one has already implemented. – Lind Apr 23 '16 at 12:55
  • +1 for the answer, atleast now I confirmed that the idea of having a reference to that one is impossible. – Lind Apr 23 '16 at 12:56
  • @Lind: "I think it counts as it exists, since the second activity is a launcher" -- the *class* might exist. The *instance* will not, until something somewhere calls `startActivity()` to cause an instance to come into being. Now, if your second activity is being started directly, and then through some form of navigation the user gets to your first activity, then an instance of that second activity class may exist. But, if the user launched your app through a *different* launcher (say, directly into the first activity), then an instance of the second activity will not exist. – CommonsWare Apr 23 '16 at 13:02
  • I agree and I understand what you are saying. But basically I think that is not possible, I am currently calling with an intent the startActivity() of the launcher from the onCreate of the primary activity of Aplication A. – Lind Apr 23 '16 at 13:51
  • What would you suggest, if you were the one in this type of situation, what approach would you take. Would you suggest on even giving up on this idea and trying a different approach ? If so, could you point me in a direction ? – Lind Apr 23 '16 at 13:52
  • 1
    @Lind: I have no further comments, beyond what I have already written. In general, if you want to integrate two things, you need to be willing to change both sides of the integration. – CommonsWare Apr 23 '16 at 14:03