I'm trying to send a variable from one class to another class created in a different package but it keeps telling me that i have unresolved reference and i can't import the variable.
This is where i try to call the variables (The ones i'm talking about are "KEY_ACCOUNT_LIST_CHANGED" and "KEY_CURRENT_ACCOUNT_CHANGED"):
if (requestCode == ACTION_MANAGE_ACCOUNTS && resultCode == RESULT_OK && data!!.getBooleanExtra(
AccountsManagementActivity.KEY_ACCOUNT_LIST_CHANGED,
false
)
) {
// current account has changed
if (data.getBooleanExtra(AccountsManagementActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
account = drawerViewModel.getCurrentAccount(this)
// Refresh dependencies to be used in selected account
initDependencyInjection()
restart()
}
As you can see, they are declaired at the class "AccountsManagementActivity" which is build this way:
abstract class AccountsManagementActivity : FileActivity(), AccountsManagementAdapter.AccountAdapterListener, AccountManagerCallback<Boolean> {
val KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED"
val KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED"
....
I've tried to delete the "abstract" from AccountsManagementActivity and then in the other class make this call:
val accountsManagementActivity = AccountsManagementActivity()
but the app crashes
i hope anyone can help me
CodePudding user response:
if you want to access variables like that you need to make them static. For kotlin you need
companion object {
val KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED"
val KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED"
}
now you can access them like this
AccountsManagementActivity.KEY_ACCOUNT_LIST_CHANGED