4

I want to retrive the name of the calling activity for checking some conditions.. What will be the solution?

3
  • 1
    activity.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. Commented 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? Commented Feb 13, 2012 at 10:24
  • Hi, Sergey Can you please tell me "activity" refers to which one? Commented Feb 13, 2012 at 10:24

3 Answers 3

6

I think you can achieve this even with getCallingActivity().getClassName()

Sign up to request clarification or add additional context in comments.

Comments

5

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

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.