how to know the calling activity in android

I have an activity which is called by few other activities. For example: I have Activity1,Activity2,Activity3.
Activity1 calls Activity2 and pass parameter.
Activity3 also calls Activity2 and pass parameter.

Now based on the calling activity, Activity2 performs some task.
But how do I know which activity is calling Activity2??
can anybody plz help me??

  • Could not find class 'android.widget.ThemedSpinnerAdapter'
  • How to automatically uninstall android app from device before installing a new version
  • Piracy, piracy, piracy. What can I do?
  • What does the Invalidate Caches/Restart do in Android Studio?
  • TextView shadow destroyed while capture image
  • Android Studio doesn't see device
  • Related posts:

    How to make Android Studio download dependencies sources and javadoc?
    How do we verify Android in-app billing receipt on the server side?
    Is it possible to use Uri.Builder and not have the “//” part?
    Multiple spinners and onItemSelected
    including list_content into list layout to retain ListFragment functionality
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android...
  • Execution failed for task ':app:compileDebugKotlin'
  • Time displayed in Logcat
  • EditText cannot be resolved to a type
  • Menu Items are not showing on Action Bar
  • Android studio getSlotFromBufferLocked: unknown buffer error
  • How to use 3G Connection in Android Application instead of Wi-fi?
  • 6 Solutions collect form web for “how to know the calling activity in android”

    A. If you can use startActivityForResult

    As per Zain Ali’s answer below: If you can start Activity with startActivityForResult() then you can get name of calling Activity class by this.getCallingActivity().getClassName();

    B. If you can not use startActivityForResult

    If you can not use startActivityForResult(), then you can use following method:
    You can pass additional parameter in intent, check the value in activity and act accordingly.

    1) Define an interface or constants class to define integer constants to indicate calling activity

    public interface ActivityConstants {
                public static final int ACTIVITY_1 = 1001;
                public static final int ACTIVITY_2 = 1002;
                public static final int ACTIVITY_3 = 1003;

    2) Add extra parameter in intent while calling Activity2.

            Intent act2 = new Intent(context, Activity2.class);
                    act2.putExtra("calling-activity", ActivityConstants.ACTIVITY_1);
        // or ActivityConstants.ACTIVITY_3 if called form Activity3

    3) Check the value of this extra parameter in Activity2 and act accordingly..

    int callingActivity = getIntent().getIntExtra("calling-activity", 0);
            switch (callingActivity) {
            case ActivityConstants.ACTIVITY_1:
                // Activity2 is started from Activity1
            case ActivityConstants.ACTIVITY_3:
                // Activity2 is started from Activity3

    If you start the activity with startActivityForResult(Intent, int), then you can get calling activity by getCallingActivity().getClassName().

    In your calling activity (FirstActivity):

    Intent i = new Intent(FirstActivity.this, SecondActivity.class);
    i.putExtra("classFrom", FirstActivity.class.toString());

    And add the following code in the onCreate of the called activity (SecondActivity):

    Bundle bundle = getIntent().getExtras();
    if (bundle.getString("classFrom").equals(FirstActivity.class.toString())) {
         //Do some task

    Notice that you should be carefully because the bundle object can’t be null when you perform “b.getString(“classFrom”)”.

    You could pass an additional parameter that specifies the calling Activity.

    I successfully use: (Activity).getLocalClassName()

    Pass anything(String/ int etc.) to putExtra and base on that do the your work like

    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
         intent.putExtra("PARENT_ACTIVITY_REF", "ParentActivityIsA");

    And then receive in child like

    String parentActivityRef = intent.getStringExtra("PARENT_ACTIVITY_REF");


    if (parentActivityRef.equals("ParentActivityIsA"){
                  // do your work here
    }else if ...{
                 // ...
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.