Search code examples
javaandroidandroid-activity

Can we know the class name of the activity that started the current?


Is there a way to know which activity class started another activity class other than passing the name of the class as an Extra in the Intent?
When we do new Intent(this, SomeOtherActivity.class) is it possible to get the name of this in the when we are in onCreate of the SomeOtherActivity?


Solution

  • Create an instance and put an extra to the intent:

    Intent i = new Intent(this, SomeOtherActivity.class)
    i.putExtra("ACTIVITY_NAME", this.getClass().getName());
    

    Retrieve it with:

    Bundle extras = getIntent().getExtras();
    String activityName = extras.getString("STRING_I_NEED");