I am trying to pass a value between two activities in kotlin but if I used the below code then I am getting only "Hello World" default value and not the PREFERENCE_NAME value. My text id name is android:id="@+id/tv_count" Any help is appreciated.
Main Activity:
import android.content.Context
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mypreference=MyPreference(this)
var loginCount=mypreference.getLoginName()
mypreference.setLoginName(loginCount)
tv_count.text=loginCount.toString()
}
}
My Preference:
import android.content.Context
class MyPreference(context:Context)
{
val PREFERENCE_NAME="SharedPreferenceExample"
val preference=context.getSharedPreferences(PREFERENCE_NAME,Context.MODE_PRIVATE)
fun getLoginName():String
{
return preference.getString(PREFERENCE_NAME,"Hello World")
}
fun setLoginName(name:String)
{
val editor=preference.edit()
editor.putString(PREFERENCE_NAME,name)
}
}