2

I have 3 activity: Activity1, Activity2, Activity3. Activity1 and Activity3 can start Activity2:

startActivity (new Intent(this,Activity2));

How in Activity2 I can check which class start Activity2 ? Is there any way to take information from Intent ? Thanks...

I dont whant to put anything in Intent. I think I dont need that. When I start intent I already saying from what context I do that (new Intent(*this*,Activity2)). And this "this" I want to recognize in Activity2 !!! Is that possible ?

Jim
  • 8,874
  • 16
  • 68
  • 125
  • Yes, you do want to put something into the `Intent` because there's no way to get at the supplied `Context` on the other side. Also, it's the Android way of doing this, don't fight it :) – Philipp Reichart Sep 15 '11 at 16:07
  • Check out my answer below for a technical explanation why your `this` is lost; too long for a comment. – Philipp Reichart Sep 15 '11 at 16:18

7 Answers7

2

You can pass that information in the "extra" Bundle of your Intent:

In Activity1:

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("caller", "activity1");
startActivity(intent);

In Activity2:

String caller = getIntent().getStringExtra("caller");

There's also get/put extra methods for other data types like boolean, int, etc.


As for why you cannot access the Context you supplied when creating the Intent on the other side:

The only thing that Android does with the supplied Context (i.e. your this) is to create a ComponentName from it. That class only keeps the package of the supplied Context and discards anything else.

So you theoretically could put your activities into different packages and then go getIntent().getComponentName().getPackageName() on it at the receiving end -- but please don't. Intent extras is the way to go.

Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
2

To send:

Intent intent = new Intent(this,Activity2)
intent.putExtra("activityNumber", 1);
startActivity(intent);

To receive:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    activitySource = extras.getInt("activityNumber");
}
BrainCrash
  • 12,992
  • 3
  • 32
  • 38
1

You can pass along information between Activities using an Intents "Extras":

Intent newIntent = new Intent(this, Activity2.class);
newIntent.putExtra("CalledFrom", Activity1.class.getSimpleName())
startActivity(newIntent);

Then in the receiving Activity you can call:

String fromActivity = getIntent().getStringExtra("CalledFrom");

You can pass basic types as well as Object's which implement the Parcelable interface

Graeme
  • 25,714
  • 24
  • 124
  • 186
0

I found the solutions that use the simple name of the class cause ClassNotFoundExceptions. The solution I use is similar, but uses the canonical name:

Intent newIntent = new Intent(this, Activity2.class);
newIntent.putExtra("caller", Activity1.class.getCanonicalName())
startActivity(newIntent);

Then in the other activity

String caller = getIntent().getStringExtra("caller");
Class callingClass = Class.forName(caller);
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
0

You can do this by adding information to your Intent.

for example:

public Intent putExtra (name, value)

In the newly started service you can then look up this information.

Or you can use the addFlags method like in the other answer.

0

Use Intent data members to pass parameters to your activities. There's an extras data member that is a Bundle of anything you want - a general-purpose name/value collection.

Intent i = new Intent(this, Activity2);
i.putExtra("From", "Activity1");
startActivity(i);

Then in Activity2 you can examine the starting intent for extra data and find this out.

Alternatively, you can use intent data, settable with setData(). Data member has the format of the URI, somewhat unfriendly to passing structured information. It's slightly less typing though.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
0

in the intent that starts activity you can always pus flag as

Intent i=new Intent();
i.putExtra("startedBy","A1");
//or 
//i.putExtra("startedBy","A2");
//...

and than in the receiving activity use in the on activityresult method

Intent intent=getIntent();
if(intent.getStringExtra("startedBy").equals("a1"){
//do somth
}
Lukap
  • 31,523
  • 64
  • 157
  • 244