How to assign same name Class variable in kotlin? - android

I want to assign my context in constructor but when I use "this" ide warn me. How can I write code like this Java code below, but in Kotlin:
here is java code
public class LoginApiService {
Context context;
public LoginApiService(Context context) {
this.context = context;
}
}
here is what I want to do
class YLAService {
var context:Context?=null
class YLAService constructor(context: Context) {
this.context=context
}
}

In Kotlin, if you provide a var or val in the constructor, it automatically becomes available as a property you can use. No other assignment is required.
class LoginApiService(val context: Context) {
// Example...
fun doSomething() {
context.doSomethingOnContext()
}
}

Related

How to get static context in FlutterPlugin

I want to set static get and set context method in FlutterPlugin, how should I implement it in kotlin way?
class FlutterPushPlugin : FlutterPlugin {
private var context: Context? = null
fun getContext(): Context? {
return this.context
}
fun setContext(context: Context?) {
this.context = context
}
}
// In another class
FlutterPushPlugin.getContext // return the context property in FlutterPushPlugin
You could use a companion object or object itself to do the job, but it is strongly not recommended to put context in static objects as this could cause memory leaks. But if you do, you have to be very careful how you use them. Now for your question:
object StaticContext {
var context: Context? = null
}
Or
class FlutterPushPlugin : FlutterPlugin {
companion object {
var context: Context? = null
}
}
There is no need for getters and setters as Kotlin generates them when using var.

How i can use context in Singleton?

I need in my Singleton -> Context. I know that I can't passing argument in constructor, because object hasn't constructor.
Then I call it from my Application class.
Here is the code:
object Singleton {
var userAgentInfo: String = UserAgentTools.buildUserAgent(context)
fun initializeSdk() {
AuthenticatorApiManager.initializeSdk(userAgentInfo)
}
}
Move the initialization of userAgentInfo to the initializeSDK method, and send the Context as an argument, make sure to send the ApplicationContext.
object Singleton {
var userAgentInfo: String? = null
fun initializeSdk(context: Context) {
userAgentInfo = UserAgentTools.buildUserAgent(context)
AuthenticatorApiManager.initializeSdk(userAgentInfo)
}
}
Make Application class and write below code.
companion object {
private lateinit var sInstance: ApplicationClass
fun getInstance(): ApplicationClass {
return sInstance
}
}
Use in object like below.
ApplicationClass.getInstance()
You can use context in your Singleton class using Application class instance.here it is

Not able to access parent class variable in child class in kotlin

In kotlin I am trying to use parent class variables in child class but I am not able to use them ,as I am new to kotlin I don't understand how to do it simply
I am trying to access sharedPerfernces and getting but its giving me null
class webViewActivity : AppCompatActivity{
internal var shared_preferences: SharedPreferences? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
shared_preferences = this.getPreferences(Context.MODE_PRIVATE)
mContext = this
}
class JavaScriptInterface(private val mContext: Context) {
#JavascriptInterface
fun exampleGet(path: String): String {
return webViewActivity().shared_preferences!!.getString(path, "")
//here shared_perferences is null
}
}
}
why I am not able to access parent class variables without constructor of parent class in child class . give me some suggestion ,little help will be appreciated
Add inner before JavaScriptInterface class.
Just like that:
inner class JavaScriptInterface(private val mContext: Context) {
#JavascriptInterface
fun exampleGet(path: String): String {
return shared_preferences!!.getString(path, "")
}
}

Using a secondary constructor in Kotlin

I get the error:
Expecting member declaration
class MyAdapter(val context: Context) {
constructor(context: Context, itemInfos: List<ItemInfo>): RecyclerView.Adapter<ContentItemViewHolder> {
}
}
What am I doing wrong?
Do something like this:
class MyAdapter(val context: Context): RecyclerView.Adapter<ContentItemViewHolder>() {
constructor(context: Context, itemInfos: List<ItemInfo>): this(context) {
}
}
If you inherit from another class you should specify it in class declaration, not constructor declaration.
You should put superclass after class declaration:
class MyAdapter(val context: Context): RecyclerView.Adapter<ContentItemViewHolder> {
constructor(context: Context, itemInfos: List<ItemInfo>): this(context) {
}
}

Cannot inject with Dagger2 on Java and Kotlin mixed project

I have a project which is primarily written in Java but I'm slowing moving to Kotlin for the new activities. This project applies Dagger2 and work perfectly when used with Java based activities. However, when I create Kotlin activity and try to inject, I get the following error.
LoginIDPresenter cannot be provided without an #Inject constructor or from an #Provides- or #Produces-annotated method. This type supports members injection but cannot be implicitly provided.
void inject(LoginIDActivity loginIDActivity);
com.maxis.mymaxis.ui.logindigitalid.LoginIDPresenter is injected at
com.maxis.mymaxis.ui.logindigitalid.LoginIDActivity.loginIDPresenter
com.maxis.mymaxis.ui.logindigitalid.LoginIDActivity is injected at
com.maxis.mymaxis.injection.component.ActivityComponent.inject(loginIDActivity)
Just to reaffirm again, when I do injection in my Java activities, it works flawlessly. Also, my Module and Component files are all in Java. Only when I create a Kotlin activity and try injecting there, I get the error.
LoginIDPresenter.kt
class LoginIDPresenter : BasePresenter<LoginIDMvpView>() {
lateinit var mContext : Context
#Inject
fun LoginIDPresenter(#ActivityContext context: Context){
mContext = context
}
override fun attachView(loginIDMvpView: LoginIDMvpView) {
super.attachView(loginIDMvpView)
}
override fun detachView() {
super.detachView()
mCompositeSubscription.clear()
}
}
LoginIDActivity.kt
class LoginIDActivity : BaseActivity(), LoginIDMvpView {
#Inject lateinit var loginIDPresenter : LoginIDPresenter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
loginIDPresenter.attachView(this)
}
override fun showErrorMessageDialog(errorObject: ErrorObject?) {
if (errorObject != null) {
Util.alertDialogBackToLandingPage(this, errorObject.getErrorUiMessage(), R.drawable.error_name)
}
}
override fun getLayoutResourceId(): Int {
return R.layout.activity_login_id
}
override fun injectActivity(component: ActivityComponent?) {
component?.inject(this)
}
override fun setToolBar() {
//no toolbar
}
}
ActivityComponent .java
#PerActivity
#Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
.
.
.
void inject(LoginIDActivity loginIDActivity);
.
.
.
}
ActivityModule.java
#Module
public class ActivityModule {
private Context mContext;
public ActivityModule(Context context) {
mContext = context;
}
#Provides
#ForActivity
Activity provideActivity() {
return (Activity) mContext;
}
#Provides
#ActivityContext
Context providesContext() {
return mContext;
}
}
Your intention is to use #Inject constructor, but you're not implementing it. Just provide a primary constructor for LoginIDPresenter:
class LoginIDPresenter #Inject constructor(
#ActivityContext val context: Context
) : BasePresenter<LoginIDMvpView>() {
override fun attachView(loginIDMvpView: LoginIDMvpView) {
super.attachView(loginIDMvpView)
}
override fun detachView() {
super.detachView()
mCompositeSubscription.clear()
}
}

Categories

Resources