0

I cannot access the hi variable from my library class. Why? Check it out:

enter image description here

I have this inteface in my library:

interface ContextAccessor {

    fun getApplicationContext(): Application?
}

And this code as well:

class SomeLibraryClass {
    private var mContextAccessor: ContextAccessor?

    String extractedHi = null

    fun setContextAccessor(contextAccessor: ContextAccessor?) {
        mContextAccessor = contextAccessor
    }
    
    fun someOtherMethod() {
        mContextAccessor?.getAppContext()?.let { nonNullContext ->
            // use nonNullContext here
            extractedHi = nonNullContext.hi; // i get error here!
        }
    }
}

And this class in my project:

public class MyActivity extends Activity implements  MyActivity.ContextAccessor {
    
    private SomeLibraryClass someLibraryClassInstance = SomeLibraryClass();

    public String hi = "hi";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ContextAccessor reference is set to some library class
        someLibraryClassInstance.setContextAccessor(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Super important!
        someLibraryClassInstance.setContextAccessor(null);
        // OR create some method like `someLibraryClassInstance.removeContextAccessor(this)`
    }

    @Override
    public Application getApplicationContext() {
        return super.getApplication();
    }
}
Sergio
  • 27,326
  • 8
  • 128
  • 149
Bandy
  • 161
  • 12
  • 1
    What's the code of `Application` and `MyActivity.ContextAccessor`? I don't see how do these things tie together. – al3c Aug 10 '20 at 16:30
  • @al3c Well, I'm just following this answer's advice: https://stackoverflow.com/a/63206457/13976983 He's not answering, so I decided to ask a question here. – Bandy Aug 10 '20 at 16:32
  • I'd appreciate it if you enlighten me. Because I'm lost. I can't see how they tie together either :( – Bandy Aug 10 '20 at 16:32
  • `getApplicationContext` returns an Application. I don't know what application looks like, you didn't post the source. You want that to have a property named `hi` I see an activity that has a `hi` public filed, but that isn't related to anything, that should be a getter called `getHi` and be part of some interface at least. – al3c Aug 10 '20 at 16:42
  • @al3c I think I almost understand. But, please, because Im a beginner, can you write a full answer with code? thank – Bandy Aug 10 '20 at 16:47
  • I simply want toretrieve "hi" from my library' – Bandy Aug 10 '20 at 16:50
  • @Brandy I can't write a full answer because I don't know what is that you're trying to achieve. Given the partial code here, It seems you want to pass a String from an Activity to some class. And that class is created inside the Activity. If that's the case just add it as a constructor parameter of your class. – al3c Aug 10 '20 at 17:04
  • please see my original question here https://stackoverflow.com/questions/63204657/access-main-project-from-module-in-android-studio/ – Bandy Aug 10 '20 at 17:39
  • @al3c see here thanks – Bandy Aug 10 '20 at 17:39

1 Answers1

0

Add hi property to the ContextAccessor interface:

interface ContextAccessor {

    val hi: String
    // ...
}

And in MyActivity implement getHi() method:

@NotNull
@Override
public String getHi() {
    return hi;
}

In your library class SomeLibraryClass you can access to it like the following:

var extractedHi: String? = null

fun someOtherMethod() {
    extractedHi = mContextAccessor?.hi
}
Sergio
  • 27,326
  • 8
  • 128
  • 149