-2

Within my Android activities I can place this code in the onCreate() method to obtain the current Activity name:-

final PackageManager packageManager = this.getPackageManager();
try {
    final ActivityInfo activityInfo = packageManager.getActivityInfo(this.getComponentName(), 0);
    Log.d(TAG, "onCreate: " + activityInfo.name);
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

Whats the equivalent code for Fragments?

e.g. What code can I place within my Fragments onAttach() method to obtain current Fragment name? Why isn't there a FragmentInfo class?

Hector
  • 4,016
  • 21
  • 112
  • 211
  • https://stackoverflow.com/questions/9294603/get-currently-displayed-fragment – Jisu Hong Aug 07 '17 at 07:56
  • When I call ActivityInfo.name() I receive "com.example.MainActivity" which looks to me like the fully qualified class name of my activity. Surely Fragments have the same thing? Fragments definitely do not have FragmentInfo. – Hector Aug 07 '17 at 07:56
  • @RayHong Im looking for an equivalent Fragment process to ActivityInfo. I do not have to do any additional set up to be able to retrieve the Activity name. I do not want to use Fragment tags, I just want to get the current fragment name within the attach method. – Hector Aug 07 '17 at 07:59
  • Why the downvote? How does downvoting with no explanation help? Why does SO allow downvoting with no explanation? Who benefits from downvoting? – Hector Aug 07 '17 at 08:23
  • What I want to achieve is discover the equivalent Fragment method/process for ActivityInfo.name(). I am curious why Activity has ActivityInfo and Fragment does not have a matching FragmentInfo. My thought process is as follows Activity has ActivityInfo why doesn't Fragment have FragmentInfo? However it looks as though (with two downvotes already) SO doesnt appreciate developers that are curios and wish to learn? – Hector Aug 07 '17 at 08:41
  • 1
    Some of us are still trying to help you so i hope you smile. – Jisu Hong Aug 07 '17 at 08:44
  • Activities and Fragments have a lifecycle and can be thought of as highlevel ui components, in fact the Google docs state "You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities)." e.g. A fragment can be thought of as a "sub Activity" where as a view is a low level UI component. – Hector Aug 07 '17 at 08:51
  • I have now qualified for two downvotes however I am still non the wiser as to why my question deserves this treatment. Can somenone explain how downvoting with no explanation adds value? – Hector Aug 07 '17 at 08:55

2 Answers2

1

in onAttach of your Fragment,

@Override
public void onAttach(Context context) {
    super.onAttatch(context);
    Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);
    Log.d(TAG, currentFragment.getClass().toString());
}
Jisu Hong
  • 724
  • 1
  • 7
  • 22
  • I do not understand how this approach resolves my issue. onAttachFragment is called when a fragment is attached as a child of this fragment. My Fragments do not have child fragments. I wish to know the current Activity & Fragment name when Fragment.onAttach() is called. – Hector Aug 07 '17 at 08:27
  • The getId() comment states "Return the identifier this fragment is known by. This is either the android:id value supplied in a layout or the container view ID supplied when adding the fragment." this results in a numeric value, I require a textual/String meaningful name. – Hector Aug 07 '17 at 08:45
  • how about currentFragment.getClass().toString() – Jisu Hong Aug 07 '17 at 08:52
0

If you add a fragment with a tag like this:

fragmentManager.beginTransaction().add(new Fragment(), "fragment-tag").commit()

You can get the tag string by

fragment.getTag()
bbsimon
  • 237
  • 2
  • 2
  • This is true, However the solution I am looking for is an equivalent Fragment process to ActivityInfo.name(). In the case of Activities I do not have to perform any "Tagging" etc. – Hector Aug 07 '17 at 08:02