question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stop accessing non-public fields in Fragment

See original GitHub issue

After updating the support library of my project to the new AndroidX scheme, the app started crashing after calling this method: BackstackAccessor.isFragmentOnBackStack(Fragment()) Crash: java.lang.IllegalAccessError: Method 'boolean androidx.fragment.app.Fragment.isInBackStack()' is inaccessible to class 'androidx.core.app.BackstackAccessor'

After some investigation, I found that the issue is this file: https://github.com/sockeqwe/mosby/blob/master/utils-fragment/src/main/java/android/support/v4/app/BackstackAccessor.java which is accessing a non-public field by “faking” the package.

As well as being a bad practice, this stops working after migrating to AndroidX due to the change of package names.

Ideally this should be reverted to the commented-out version of the code still existing in the linked file.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:25 (13 by maintainers)

github_iconTop GitHub Comments

11reactions
tommuscommented, Sep 23, 2018

AndroidX is now stable 1.0.0. Even more, Google wrote that the Support 28.0.0 is the last version that supports android.support package. It is time to move on.

6reactions
drampeltcommented, Jul 9, 2018

As a temporary workaround until then for those who want to continue using AndroidX, you can exclude the utils-fragment module dependency in your build.gradle:

compile 'com.hannesdorfmann.mosby3:mvi:3.1.0' {
    exclude group: 'com.hannesdorfmann.mosby3', module: 'utils-fragment'
}

and then define your own androidx.core.app.BackstackAccessor class:

package androidx.core.app;

import androidx.fragment.app.Fragment;

public class BackstackAccessor {
    public static boolean isFragmentOnBackStack(Fragment fragment) {
        return false;
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to disable access to fragment's layout(views) from ...
I'm working on a library which will provide fragment with some input fields in it. These input fields will contain user's private information...
Read more >
Fragment | Android Developers
Though Fragment defines its own lifecycle, that lifecycle is dependent on its activity: if the activity is stopped, no fragments inside of it...
Read more >
Building dynamic user interfaces in Android with fragments
The FragmentManager class allows you to add, remove and replace fragments in the layout of your activity. It can accessed in an activity...
Read more >
Working with Fragments in Thymeleaf - Baeldung
Note that if we're using Spring Boot, this configuration may not be ... <th:block th:include="fragments/general.html :: headerfiles"> ...
Read more >
Working with Content Fragments | Adobe Experience Manager
Content fragments can also be delivered in JSON format, using the Sling Model (JSON) export capabilities of AEM core components. This form of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found