I want to retrive the name of the calling activity for checking some conditions.. What will be the solution?
-
1activity.class.getName() ??? if you calling another activity from activity you might want to supply it's name into a Bundle and then start the Intent. – Sergey Benner Feb 13 '12 at 09:58
-
have i understood the answers to this question correctly that I (as an activity or as service) cannot find out who (activity or service) wants to use me without cooperation of the caller? – k3b Feb 13 '12 at 10:24
-
Hi, Sergey Can you please tell me "activity" refers to which one? – Ranjit Feb 13 '12 at 10:24
3 Answers
You can send data between activities using Bundle
Have a look at this example: http://www.balistupa.com/blog/2009/08/passing-data-or-parameter-to-another-activity-android/
Basically you need to put the name of the caller as parameter:
Bundle bundle = new Bundle();
bundle.putString(this.class.getName(), “ClassName”);
Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
And in ActivityClass2, you can read this parameter using:
Bundle bundle = this.getIntent().getExtras();
String className = bundle.getString(“ClassName″);

- 57,267
- 35
- 174
- 180
Step1: Create your interface->right button your project->New->JavaClass->Kind=Interface
public interface ActivityConstants {
public static final int NameFromTheFirstActivity = 1001;
public static final int NameFromTheSecondActivity = 1002;
public static final int NameFromTheThirdActivity = 1003;
}
Step2: Into each Activities (Activity1 or Activity2 or Activity3) when u build your intent to call the ActivityFromWhereYouWantChooseTheAction (called ActivityChooseAction) you need to put an Extras like... Into Activity1 write this:
Intent intent = new Intent(getApplicationContext(), ActivityChooseAction.class);
intent.putExtra("calling-activity", ActivityConstants.NameFromTheFirstActivity);
startActivity(intent);
Into Activity2 write this:
Intent intent = new Intent(getApplicationContext(), ActivityChooseAction.class);
intent.putExtra("calling-activity", ActivityConstants.NameFromTheSecondActivity);
startActivity(intent);
Into Activity3 write this:
Intent intent = new Intent(getApplicationContext(), ActivityChooseAction.class);
intent.putExtra("calling-activity", ActivityConstants.NameFromTheThirdActivity);
startActivity(intent);
After you can go into your ActivityChooseAction and write this:
Public class ActivityChooseAction...{
String parametersharedtouseinthisactivity="";
...
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitychooseaction_layout);
mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
int callingActivity = getIntent().getIntExtra("calling-activity", 0);
switch (callingActivity) {
case ActivityConstants.NameFromTheFirstActivity:
//write your parameter....if u have saved it into preferences you can do like
parametersharedtouseinthisactivity = mPreferences.getString("nameparameter", defaultvalueyouwant);
break;
case ActivityConstants.NameFromTheSecondActivity:
parametersharedtouseinthisactivity = mPreferences.getString("nameparameter", defaultvalueyouwant);
break;
case ActivityConstants.NameFromTheThridActivity:
parametersharedtouseinthisactivity = mPreferences.getString("nameparameter", defaultvalueyouwant);
break;
}
}
I hope this could help someone, I followed what the user here said: how to know the calling activity in android

- 1
- 1

- 581
- 7
- 15