Ranjit
Ranjit

Reputation: 209

How to get the name of calling activity?

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

Upvotes: 4

Views: 7502

Answers (3)

Alexiscanny
Alexiscanny

Reputation: 579

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

Upvotes: 1

Olsi Saqe
Olsi Saqe

Reputation: 383

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

Upvotes: 6

Caner
Caner

Reputation: 59188

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″);

Upvotes: 5

Related Questions