7

I would like to compare the calling activity to others to know which one called the current activity. I tried:

getCallingActivity().getClassName().toString().equals(MainActivity.class.toString()) 

It doesn't work, except by passing a value in the calling Intent, how can we compare classes using getCallingActivity() or getCallingPackage()?

3
  • Hey @JerecTheSith how you solve this problem ? Commented Aug 12, 2016 at 7:29
  • 1
    Hey @HarinKaklotar you might want to check this out, I don't remember how I did it back in 2013 : programcreek.com/java-api-examples/… Commented Aug 12, 2016 at 8:55
  • thanks @JerecTheSith Commented Aug 12, 2016 at 12:53

1 Answer 1

9

You can compare the calling activity in the following manner if the activity is started for result from another activity.

 if (getCallingActivity().getClassName().equals(MainActivity.class.getName())) { /* here the main activity is calling activity*/ }else{ /*other activity is calling activity*/ } 
Sign up to request clarification or add additional context in comments.

7 Comments

hey i try this logic but it give me exaption like this: Attempt to invoke virtual method 'java.lang.String android.content.ComponentName.getClassName()' on a null object reference
did u started activity for result?? if not then getCallingActivity() will return null which will cause null pointer
if (getCallingActivity().getClassName().equals(PlayerActivity.class.getCanonicalName())) code = 100; else { code = isPlaylist.equals("True") ? 100 : 200; } setResult(code, in);
use getName() instead of getCanonicalName() see the differences here stackoverflow.com/questions/15202997/…
then the activity is not started for result. start it with startActivityForResult then everything will be fine
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.