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– Sergey Benner2012-02-13 09:58:10 +00:00Commented Feb 13, 2012 at 9: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– k3b2012-02-13 10:24:15 +00:00Commented Feb 13, 2012 at 10:24
- Hi, Sergey Can you please tell me "activity" refers to which one?Ranjit– Ranjit2012-02-13 10:24:38 +00:00Commented Feb 13, 2012 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″); Comments
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