I need to edit a editText which is on main_layout from a fragment instead of MainActivity().
I've tried inflating main_layout to fragment but that doesn't worked (editText doesn't change), then I've tried to create method
fun changeEditText(){
editText.setText(R.string.name)
}
but when I call it in my Fragment using
MainActivity().changeEditText()
it gives me this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
How can I edit this editText from fragment?
You can't initialize an activity via its constructors. You can call the method from fragment like below
((MainActivity)getActivity()).changeEditText();
You can call getActivity() from the Fragment to get a reference to its parent activity:
(activity as? MainActivity)?.changeEditText()
However, a better approach would be to use a listener so the fragment doesn't care what it's parent activity has implemented:
interface Listener {
fun onTextChanged()
}
fun changeEditText() {
editText.setText(R.string.name)
listener?.onTextChanged()
}
class MainActivity : AppCompatActivity() {
//override ......
fun changeText() {
EditText editText = findViewById(R.id.my_edittext)
edittext.text = "something"
}
}
class MyFragment : Fragment() {
private lateinit var hostActivity: AppCompatActivity
override fun onAttach(context: Context){
hostActivity = context as AppCompatActivity
}
override fun onViewCreated(){
hostActivity.changeText()
}
}
Or
class MainActivity :AppCompatActivity(), MyCallback() {
// override ......
override fun onTextChange(){
val editText = findViewById(R.id.my_edittext)
edittext.text = "something"
}
}
class MyFragment : Fragment() {
private lateinit var myCallback: MyCallback
override fun onAttach(context: Context){
myCallback = context as MyCallback
}
override fun onViewCreated(){
myCallback.onTextChange()
}
}
interface MyCallback {
fun onTextChange()
}
Or
class MainActivity :AppCompatActivity(), MyCallback() {
override fun onCreate(savedInstanceState: Bundle?){
val sharedViewmodel = ViewmodelProviders.of(this).get(SharedViewModel.class)
sharedViewmodel.text.observe(this, object: Observer<String> {
override fun onChanged(text: String?){
val editText = findViewById(R.id.my_edittext)
edittext.text = text
}
})
}
}
class MyFragment : Fragment() {
private lateinit var hostActivity: AppCompatActivity
override fun onAttach(context: Context){
hostActivity = context as AppCompatActivity
}
override fun onViewCreated(){
val sharedViewmodel = ViewmodelProviders.of(hostActivity).get(SharedViewModel.class)
sharedViewmodel.text = "My new text"
}
}
class sharedViewModel: ViewModel(){
private val textHolderLiveData = MutableLiveData<String>()
fun getText(): LiveData<String> {
return textHolderLiveData
}
fun setText(text: String) {
textHolderLiveData.value = text
}
}
Related
I want to use mvvm in Dialog but I don't know how to pass LifecycleOwner to observe
class CommonDialog(context: Context) : Dialog(context, R.style.AppMaskStatusTheme) {
private val viewBinding: DialogSportOrderBinding by lazy { DialogSportOrderBinding.inflate(LayoutInflater.from(context)) }
private val viewModel by lazy { ViewModelProvider(context as ViewModelStoreOwner)[SportOrderViewModel::class.java] }
init {
setContentView(viewBinding.root)
viewModel.sportOrderList.observe(***what to pass here?***, androidx.lifecycle.Observer {
})
}
}
I have tried context as LifecycleOwner, context as AppCompatActivity but all fail
please help me, thanks!
You can create your own LifecycleOwner like this
class MyLifecycleOwner() : LifecycleOwner {
private val mLifecycleRegistry: LifecycleRegistry by lazy { LifecycleRegistry(this) }
init {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
}
fun stop() {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
}
fun start() {
mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
}
override fun getLifecycle(): Lifecycle = mLifecycleRegistry
}
Then your Dialog class will be like this
class CommonDialog(context: Context) : Dialog(context, R.style.AppMaskStatusTheme) {
private val viewBinding: DialogSportOrderBinding by lazy { DialogSportOrderBinding.inflate(LayoutInflater.from(context)) }
private val viewModel by lazy { ViewModelProvider(context as ViewModelStoreOwner)[SportOrderViewModel::class.java] }
private val lifeCycleOwner: MyLifecycleOwner by lazy { MyLifecycleOwner() }
init {
setContentView(viewBinding.root)
viewModel.sportOrderList.observe(lifeCycleOwner, {
})
}
override fun onStart() {
super.onStart()
lifeCycleOwner.start()
}
override fun onStop() {
super.onStop()
lifeCycleOwner.stop()
}
}
Read more about Lifecycle-Aware Components at here
android.content.Context does not implement android.arch.lifecycle.LifecycleOwner.
You'd have to pass an instance of AppCompatActivity, which implements android.arch.lifecycle.LifecycleOwner (or any other class which does that).
or cast (AppCompatActivity) context, when context is an instanceof AppCompatActivity.
I'm trying to handle fragment clicks in FragmentPagerAdapter, but sometimes I'm getting Fatal Exception: kotlin.UninitializedPropertyAccessException, which says that click listener property is not initialized.
So here is code for the PagerAdapter
class ApplicationListPagerAdapter(
fm: FragmentManager,
private val onListItemClick: (isSent: Boolean, application: Application) -> Unit,
private val onGoToScholarshipsTabClicked: () -> Unit,
private val onGoToPicksTabClicked: () -> Unit
): FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT),
ApplicationListFragment.ClickListener {
override fun getCount(): Int {
return 2
}
override fun getItem(position: Int): Fragment {
val isSent = position == 1
val fragment = ApplicationListFragment.newInstance(isSent)
fragment.setApplicationSelectListener(this)
return fragment
}
override fun getPageTitle(position: Int): CharSequence? {
return if (position == 0) "PICKS" else "SENT"
}
override fun applicationSelected(isSent: Boolean, application: Application) {
onListItemClick(isSent, application)
}
override fun goToScholarshipsTabClicked() {
onGoToScholarshipsTabClicked()
}
override fun goToPicksTabClicked() {
onGoToPicksTabClicked()
}
}
Code how I initialize it in fragment
private lateinit var clickListener: ClickListener
fun setApplicationSelectListener(clickListener: ClickListener) {
this.clickListener = clickListener
}
interface ClickListener {
fun applicationSelected(isSent: Boolean, application: Application)
fun goToScholarshipsTabClicked()
fun goToPicksTabClicked()
}
And here in onClick callback I'm getting crashes sometimes for some users.
private fun initRecyclerView(applications: List<Application>) {
application_list_recyclerView.adapter = ApplicationListItemsAdapter(
context!!,
applications.toMutableList(),
isSent,
this,
applicationViewModel.applicationService,
onClick = {
clickListener.applicationSelected(isSent, it)
},
onDelete = { application: Application, count: Int ->
scholarshipViewModel.unFavoriteScholarship(application.scholarship)
sharedViewModel.deletePickedScholarship(application.scholarship)
applicationSharedViewModel.updatePicksCount(count)
if (count == 0) {
showNoApplicationsFragment()
}
},
onUndoDelete = { application: Application, count: Int ->
sharedViewModel.undoDeletedScholarship(application.scholarship)
applicationSharedViewModel.updatePicksCount(count)
if (count == 1) {
hideNoApplicationsFragment()
}
}
)
application_list_recyclerView.layoutManager = LinearLayoutManager(context!!)
}
Thanks for any advice and I hope my explanation makes senes for everybody
Interface
interface OnItemClickListener {
//just a random method
fun itemClicked(positon: Int)
}
Inside Your Adapter class
private lateinit var listener: OnItemClickListener
fun attachListener(listener: OnItemClickListener){
this.listener = listener
}
Acticity/ Fragment
class MainActivity(): AppCompatActivity(), OnItemClickListener{
// implement interface in case you wanna use methods...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chat)
// calling the listener method
myAdapter.attachListener(this)
}
}
above myAdapter is your adapter must be init first which is associate to your adapter class
I am trying to execute a method(fun onCreateViewModel()) in viewModel whenever the onCreate of the Activity is invoked. But the method is not getting executed.
ViewModel
class MainActivityViewModel(startingCount : Int) : ViewModel(), LifecycleObserver {
var count = 0
init {
count = startingCount
}
fun getCurrentCount(): Int{
return count
}
fun getUpdatedCount(): Int {
count ++
return count
}
#OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public fun onCreateViewModel(){
Log.i("Jts"," ViewModel created")
}
}
Activity
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var user = User("Jeffin T", "26")
private lateinit var mainActivityViewModel: MainActivityViewModel
private lateinit var mainActivityViewModelFactory: MainActivityViewModelFactory
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// mainActivityViewModel = ViewModelProviders.of()
mainActivityViewModelFactory = MainActivityViewModelFactory(123)
mainActivityViewModel = ViewModelProviders.of(this, mainActivityViewModelFactory)
.get(MainActivityViewModel::class.java)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.tvName.text = mainActivityViewModel.getCurrentCount().toString()
binding.user = user
binding.btnName.setOnClickListener {
binding.tvName.text = mainActivityViewModel.getUpdatedCount().toString()
}
}
private fun displayGreeting() {
binding.apply { tvName.setText("Hello! " + user?.name) }
}
}
You have to add the viewmodel as an observer of the lifecycle of your activity. You need to add this in your Activity
getLifecycle().addObserver(viewModel)
More info here
Hi I have LoginActivity and LoginViewModel and some more classes. I have showLoading and hideLoading in the BaseActivity so it can be accessible from each activity.
I am able to call LoginActivity method from the LoginViewModel like mNavigator?.startForgotPasswordActivity()
I want to call it from the LoginViewModel then what the way to do it using MVVM ? or I am going with wrong approach. Please suggest what is the correct way to do this ?
BaseActivity.kt
abstract class BaseActivity : AppCompatActivity(), AnkoLogger {
private val progressBar: ProgressBar? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
protected fun getToolbar(): Toolbar {
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
return toolbar
}
protected fun performDependencyInjection() {
AndroidInjection.inject(this);
}
#TargetApi(Build.VERSION_CODES.M)
fun requestPermissionsSafely(permissions: Array<String>, requestCode: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(permissions, requestCode)
}
}
#TargetApi(Build.VERSION_CODES.M)
fun hasPermission(permission: String): Boolean {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
}
fun isNetworkConnected(): Boolean {
return NetworkUtils.isNetworkConnected(applicationContext)
}
fun showLoading() {
hideLoading()
// show progress bar
}
fun hideLoading() {
// hide progress bar
}
}
LoginActivity.kt
class LoginActivity : BaseActivity(), LoginNavigator {
#Inject
lateinit var loginViewModel: LoginViewModel
override fun onCreate(savedInstanceState: Bundle?) {
performDependencyInjection()
super.onCreate(savedInstanceState)
val activityLoginBinding: ActivityLoginBinding = DataBindingUtil.setContentView<ActivityLoginBinding>(this, R.layout.activity_login)
activityLoginBinding.loginViewModel = loginViewModel
loginViewModel.mNavigator = this
}
override fun startHomeActivity() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun startRegistrationActivity() {
startActivity(Intent(this, RegistrationActivity::class.java))
}
override fun startForgotPasswordActivity() {
startActivity(Intent(this, ForgotPasswordActivity::class.java))
}
override fun handleError(throwable: Throwable) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
LoginViewModel.kt
class LoginViewModel : BaseViewModel<LoginNavigator>(), AnkoLogger {
val emailField = ObservableField<String>()
private val email: String
get() = emailField.get()
val passwordField = ObservableField<String>()
private val password: String
get() = passwordField.get()
#Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
fun login(view: View) {
if (isEmailAndPasswordValid(email, password)) {
ApiHelperImpl().doServerLoginApiCall(email, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object : CallbackWrapper<LoginResponse>() {
override fun onSuccess(loginResponse: LoginResponse) {
info { loginResponse }
}
})
}
}
/**
* Validate email and password. It checks email and password is empty or not
* and validate email address is correct or not
* #param email email address for login
* #param password password for login
* #return true if email and password pass all conditions else false
*/
private fun isEmailAndPasswordValid(email: String, password: String): Boolean {
if (email.isEmpty()) return false
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) return false
if (password.isEmpty()) return false
return true
}
}
BaseViewModel.kt
abstract class BaseViewModel<N> {
var mNavigator: N? = null
}
There can be 2 approach for same
1) Use all functions i.e related to UI update or UI event listener from a view (Activity or Fragment) according to mvp and from viewmodel only try to manage data like api's and other logic
class LoginActivity : BaseActivity(), LoginNavigator {
#Inject
lateinit var mLoginViewModel: LoginViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
mLoginViewModel.mNavigator = this
callApi()
}
private fun callApi() {
showLoading()
mLoginViewModel.callApi()
}
override fun openHomeScreen(model: Model) {
hideLoading()
showSnackBar(constraint_root, model.Domain)
}
}
class LoginViewModel(sessionManager: SessionManager, requestInterface: RequestInterface) : BaseViewModel<LoginNavigator>(sessionManager, requestInterface) {
fun callApi() {
requestInterface.getServiceIP()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResponse, this::handleError)
}
private fun handleResponse(model: Model) {
if (model.Domain == null) {
mNavigator!!.openHomeScreen(model)
} else {
}
}
private fun handleError(error: Throwable) {
error.printStackTrace()
}
}
2)
In Login interface add a function
interface LoginNavigator {
fun openHomeScreen()
fun getActivity(): BaseActivity
}
In LoginActivity override the function and return
override fun getActivity(): BaseActivity = this
Now using navigator you can access base activity & call show/hide loader function
mNavigator!!.getActivity().showLoading()
The best practice on Android for creating a Fragment is to use a static factory method and pass arguments in a Bundle via setArguments().
In Java, this is done something like:
public class MyFragment extends Fragment {
static MyFragment newInstance(int foo) {
Bundle args = new Bundle();
args.putInt("foo", foo);
MyFragment fragment = new MyFragment();
fragment.setArguments(args);
return fragment;
}
}
In Kotlin this converts to:
class MyFragment : Fragment() {
companion object {
fun newInstance(foo: Int): MyFragment {
val args = Bundle()
args.putInt("foo", foo)
val fragment = MyFragment()
fragment.arguments = args
return fragment
}
}
}
This makes sense to support interop with Java so it can still be called via MyFragment.newInstance(...), but is there a more idiomatic way to do this in Kotlin if we don't need to worry about Java interop?
I like to do it this way:
companion object {
private const val MY_BOOLEAN = "my_boolean"
private const val MY_INT = "my_int"
fun newInstance(aBoolean: Boolean, anInt: Int) = MyFragment().apply {
arguments = Bundle(2).apply {
putBoolean(MY_BOOLEAN, aBoolean)
putInt(MY_INT, anInt)
}
}
}
Edit: with KotlinX extensions, you can also do this
companion object {
private const val MY_BOOLEAN = "my_boolean"
private const val MY_INT = "my_int"
fun newInstance(aBoolean: Boolean, anInt: Int) = MyFragment().apply {
arguments = bundleOf(
MY_BOOLEAN to aBoolean,
MY_INT to anInt)
}
}
inline fun <reified T : Fragment>
newFragmentInstance(vararg params: Pair<String, Any>) =
T::class.java.newInstance().apply {
arguments = bundleOf(*params)
}`
So it is used like that:
val fragment = newFragmentInstance<YourFragment>("key" to value)
Credit
bundleOf() can be taken from Anko
Late to the party, but I believe Idiomatically it should be something like this:
private const val FOO = "foo"
private const val BAR = "bar"
class MyFragment : Fragment() {
companion object {
fun newInstance(foo: Int, bar: String) = MyFragment().withArgs {
putInt(FOO, foo)
putString(BAR, bar)
}
}
}
With an extension like this:
inline fun <T : Fragment> T.withArgs(argsBuilder: Bundle.() -> Unit): T =
this.apply {
arguments = Bundle().apply(argsBuilder)
}
or
companion object {
fun newInstance(foo: Int, bar: String) = MyFragment().apply {
arguments = bundleOf(
FOO to foo,
BAR to bar
)
}
}
The key being that the private constants should not be part of the companion object.
companion object {
private const val NOTE_ID = "NOTE_ID"
fun newInstance(noteId: Int?) = AddNoteFragment().apply {
arguments =
Bundle().apply { putInt(NOTE_ID, noteId ?: Int.MIN_VALUE) }
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
arguments?.let {
noteId = it.getInt(NOTE_ID)
}
}
Another way of doing this I found here
class MyFragment: Fragment(){
companion object{
private val ARG_CAUGHT = "myFragment_caught"
fun newInstance(caught: Pokemon):MyFragment{
val args: Bundle = Bundle()
args.putSerializable(ARG_CAUGHT, caught)
val fragment = MyFragment()
fragment.arguments = args
return fragment
}
...
}
...
}
More elegant way in my opinion
open class Instance<T : Fragment> {
#Suppress("UNCHECKED_CAST")
fun newInstance(vararg args: Pair<String, Any?>): T {
val cls = Class.forName(javaClass.name.substringBefore("$"))
return (cls.newInstance() as T).apply {
arguments = bundleOf(*args)
}
}
}
class MyFragment : Fragment() {
companion object : Instance<MyFragment>()
}
Keep in mind to add proguard rule to save constructor
-keepclassmembers class * extends androidx.fragment.app.Fragment {
<init>(...);
}
Or without reflection and proguard
open class Instance<T : Fragment>(private val cls: Class<T>) {
fun newInstance(vararg args: Pair<String, Any?>): T {
return cls.newInstance().apply {
arguments = bundleOf(*args)
}
}
}
class MyFragment : Fragment() {
companion object : Instance<MyFragment>(MyFragment::class.java)
}
Example of usage
val myFragment = MyFragment.newInstance("foo" to "bar)
Kotlin package-level function
What about about that kotlin says to use package level function instead of “static” method
MyFragment.kt
class MyFragment : Fragment() {
.....
}
fun MyFragmentNewInstance(): MyFragment {
return MyFragment()
}
MyActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (supportFragmentManager.findFragmentById(R.id.fragmentContainer) == null) {
supportFragmentManager.beginTransaction()
.add(R.id.fragmentContainer, MyFragmentNewInstance())
.commit()
}
}