1

I'm looking for a way to get the activity (A) which started my current activity (B).

So A calls:

Intent intent = new Intent(this, B.class);
startActivity(intent);

I'm looking for some method I can call in B to get A, so something like (in B):

Activity a = someMethodToGetCallingActivity();

Any ideas?

CLOSING EDIT:

As all the comments below say, I'm trying to use Android incorrectly. I was looking to get the instance of the activity A, but since there's no guarantee it exists, I can't. It's not a duplicate of the other question suggested because they were trying to work out which Activity (out of many) fired off the current Activity, not trying to get the instance of a particular Activity. Thanks all :)

LomaxOnTheRun
  • 592
  • 1
  • 8
  • 21
  • 2
    It's better to use Fragment within Activity A in this case, otherwise the communication channel will be extremely unreliable since the activity A will be paused and it can possibly be destroyed by the system – Chaosit Jul 24 '15 at 12:49
  • 1
    Agreed. Activity A does not have to exist. And, even if it does, you have no way, short of #EpicFail memory leaks, to get access to an instance of it. Solve your problem in some other way (e.g., event bus). – CommonsWare Jul 24 '15 at 12:50
  • You want to get the name of the calling activity or the activity itself (I mean the activity object)? – Joaquin Iurchuk Jul 24 '15 at 12:50
  • possible duplicate of [how to know the calling activity in android](http://stackoverflow.com/questions/4967799/how-to-know-the-calling-activity-in-android) – Mauker Jul 24 '15 at 12:53
  • I guess he wants the Activity object... but, what for? – Mauker Jul 24 '15 at 12:54
  • @Mauker, it was to attach a listener to that Activity, so that when my background process did a certain thing, it would alert the Activity. – LomaxOnTheRun Jul 24 '15 at 14:21

4 Answers4

4

what about passing the activity A name in the intent data and storing it in Activity B when it start?

Lary Ciminera
  • 1,270
  • 8
  • 15
0

There is no way to get intent you came from. You can pass intent nam as param or try to invoke some special method

Fixus
  • 4,631
  • 10
  • 38
  • 67
  • What about `getIntent()` method of activity class ? It do return the intent that start the current activity. However it will not help getting the instance of the activity who fired the intent. – sonic Jul 24 '15 at 12:55
  • maybe I'm missing something. You want to get the name of the Activity or the object or what ? – Fixus Jul 24 '15 at 13:00
0

Why don't you use intent.putExtras... And at next activity(B) just get that intent by getIntent and use get...Extra to get that data?

ULHAS PATIL
  • 862
  • 8
  • 19
0

You can not get intent of Activity A (previous Activity) yet you can pass the details of intent via bundle to Activity B.

DaniZ
  • 596
  • 4
  • 4