0

Is there any way for a Java class to know if it was called from a Fragment, or from an Activity? I'm programming for Android, if it makes any difference

n00b programmer
  • 2,671
  • 7
  • 41
  • 56
  • 1
    As far as I know, in Java, an object cannot know from where its methods are being called. What do you mean by an Android Class? – Basant Singh Mar 15 '14 at 14:12
  • I guess that answers my question.. I meant a Java class of course, but I'm programming specifically for Android. I'll edit the question – n00b programmer Mar 15 '14 at 14:13
  • parse Thread.currentThread().getStackTrace() if you are really desperate :) Nutomic's answer of passing some variable is much better, assuming it's possible for you. – NameSpace Mar 15 '14 at 14:22

3 Answers3

1

If you can change the calling code, you can always use something like the following:

public class MyClass {
    public MyClass(boolean fromActivity) {
        if (fromActivity) {
            // handle call from activity
        }
        else {
            // handle call from fragment
        }
    }
}

If you have anything other than Acitivities/Fragments calling your code, you could always use an enum.

  • That much I know, I just prefer to change as little as possible, especially in classes that extend other classes. But thanks! – n00b programmer Mar 15 '14 at 14:22
  • There are some ways to do this [https://stackoverflow.com/questions/1007268/how-do-i-find-out-the-calling-class](https://stackoverflow.com/questions/1007268/how-do-i-find-out-the-calling-class), but I would use that only for debugging or logging. –  Mar 15 '14 at 14:25
1

I agree with @basant_matharu's comment in your post.

Even if we can't determine that, it doesn't mean that we can't really know if it was an activity or fragment.

I may be wrong here and it may be totally out of question. But, what if, it's just you who want's to know if it was an activity or fragment. In that case, can't we just pass two results; one from activity and another from fragment to suppose in some another activity and just determine if the result came from an activity or from a fragment.

For instance;

We pass some pass or just use some preference to store a value from ActivityA : maybe a string result.

We do the same from fragment and just use a preference in fragment too. : maybe a string result again.

Now, in the third activity, we get two values; one from activity and one from fragment.

After that we just compare the results and see where the value came from. Like,

If the value came from activity, String resultfromactivity; return activity(SomeStringPlaceholder) and do some code here.... and same compare with Fragment.

In that way we just know if the value came from Activity or Fragment.

Again, I may be wrong here..So, if someone wants to edit or make suggestion to this, I will be happy to accept that.

mike20132013
  • 5,357
  • 3
  • 31
  • 41
1

This is possible by checking the call stack - check this question to read how to get know how to do it: Get current stack trace in Java You can put it into your class constructor and just check what called it.

However:

This is usually not a good way to write programs. The better way to achieve it is just to add some parameter in constructor, create factory method for class or use one of may other possibilities that are not against common used programming patterns.

Community
  • 1
  • 1
piotrpo
  • 12,398
  • 7
  • 42
  • 58