I'm currently having two activities, MainActivity and ResultActivity and both of them will use the same fragment - Favorite. my question is: How to know which activity is containing the fragment? For example I have a fragment A inside activity ResultActivity, and how can I know if fragment A is contained in ResultActivity or MainActivity?
get parent activity by a fragment
1.3k Views Asked by xxddd_69 At
2
There are 2 best solutions below
3
Nhân Khuất Văn
On
I think this is the best practice for you.
And to get Activity you should use requireActivity()
And to get Context you should use requireContext()
// for kotlin
fun isMainActivity(): Boolean {
return requireActivity() is MainActivity
}
public boolean isMainActivity() {
return requireActivity() instanceof MainActivity;
}
Related Questions in ANDROID
- Delay in loading Html Page(WebView) from assets folder in real android device
- MPAndroidChart method setWordWrapEnabled() not found
- Designing a 'new post' android activity
- Android :EditText inside ListView always update first item in the listview
- Android: Transferring Data via ContentIntent
- Wrong xml being inflated android
- AsyncTask Class
- Unable to receive extras in Android Intent
- Website zoomed out on Android default browser
- Square FloatingActionButton with Android Design Library
- Google Maps API Re-size
- Push toolbar content below statusbar
- Android FragmentPagerAdapter Circular listview
- Layout not shifting up when keyboard is open
- auDIO_OUTPUT_FLAG_FAST denied by client can't connect to localhost
Related Questions in ANDROID-FRAGMENTS
- Android - No view found for fragment
- Fragment AsyncTask not executing after back button pressed
- I want to Show Simple Toast Message When User Click On Media Controller button Pause/Start/Stop in video View Using Media Controller
- Call special fragment in Navigation Drawer Activity from separate Activity
- Android FragmentViewPager in Fragment
- How to restrict fragment from adding to top of stack if it already exist in the top of stack in android
- Android Studio - Illegal character 8204 error
- Adapter ListView not refreshing from Activity
- Fragment's Transaction replace on API-21 is staying behind
- Integrating Localytics - FragmentActivity
- Null pointer exception on view pager swipe
- Error:(24, 51) error: incompatible types: FragmentAcitvity cannot be converted to Fragment
- How to get menu options in actionbar aligned with fragments?
- Using Edittext in Fragment Activities outside the onCreateView method
- Clicking CardView to load a fragment
Related Questions in ANDROIDX
- MotionLayout with recyclerview make onBindViewHolder get called many many times
- Border not appearing around MaterialCardView
- Why can't migrate my flutter project to androidx?
- Can I mix SingleChildScrollview with Pageview
- Bridging Android CameraX into React Native
- Appcompat activity along with androidx gradle version issue in android studio
- AndroidX - Simulate Preference Click Programmatically
- Is there an annotation that denotes a max Android API version?
- error: cannot find symbol import androidx.core.widget.SwipeRefreshLayout; after migrating to androidx
- Annotation process feature not working since androidX upgrade
- Detect swiping out of bounds in Android's ViewPager2
- gradle fail to sync due to dependencies fail
- Custom Row Adapter in androidx.preference.ListPreference
- Closed :Android: App keeps crashing when using com.google.android.material below android 9 (sdk 29)
- How to detect when user pastes something when application is running in the background?
Related Questions in ANDROID-API-30
- Android >= 11 navigate between folder to find file without manage_external_storage permission
- Push notifications not working in android 12
- Why does the navigationBars() come back on Fullscreen activity?
- How use "android.net.wifi.NetworkSpecifier.Builder" with jnius?
- Android intent.resolveActivity returns null in API 30
- Cordova [Android platform] migration from API 29 to API 30
- Can I start my app after booting on Android API 30?
- get parent activity by a fragment
- SQLiteDatabase Access / API 30
- Pixel 6 Pro Android emulator shows only the frame but not the screen
- I need to install Android API 30. But I cannot see that API in the SDK platform list in Android Studio. How can I install it?
- How delete Folder on Android API 30 and API 29
- How to change status bar color of AppCompatDialogFragment in Android 11 (API level 30)
- How can I use hardwarepropertiesmanager(android) in android api 30, android 11
- Cannot kill recent task manager when Make home launcher application
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You can check an instance of this activity.
getActivity() - can return null value, so you need check this.
Java
Kotlin
Edited: you can also use another method requireActivity the difference is that you receive notNull value, but if activity null you can get IllegalStateException.
requireActivity() - return notNull value but can throw Exception.
Kotlin