Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the name of the activity from the fragment?

So basically i am going to use one Fragment in two different activities.Except one method of fragment in which I want to change something.So how do i get the name of activity which is using the Fragment so that I can do things depending upon the name of which activity is current.

like image 280
sharin gan Avatar asked Dec 24 '15 05:12

sharin gan


2 Answers

in Java try:

getActivity().getClass().getSimpleName() 
  • But be careful when you're using getActivity() method from fragment. If your fragment is not attached to activity getActivity() will return null.

in Kotlin try:

activity?.javaClass?.simpleName
  • It's null safe
like image 134
savepopulation Avatar answered Oct 12 '22 09:10

savepopulation


The first Ans is great but it's in java so i translate this in koltin

activity?.javaClass?.simpleName
like image 30
Hamza Avatar answered Oct 12 '22 10:10

Hamza