I'm trying to create a dialog inside a callback onSuccess() which again is inside the onCreate() method of the activity, but the dialog view doesn't show up.
When I call createDialog() directly inside the onCreate() it works. What could be the reason why it's not working in the onSuccess() callback function? onSuccess() and createDialog() definitely get called because the println show up.
Code:
class BleDevicesControlActivity : AppCompatActivity() {
private var device: BluetoothDevice? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_ble_devices_control)
connectToDevice(object : BleCommunication.OnConnectionListener {
override fun onSuccess() {
println("onSuccess called")
createDialog()
}
override fun onFailure() {
println("onFailure called")
}
})
}
private fun connectToDevice(onConnectionListener : BleCommunication.OnConnectionListener) {
bleCommunication.connect(device!!, onConnectionListener)
}
private fun createDialog() {
println("createDialog called")
val dialogInflater = LayoutInflater.from(this)
val alertDialogView = dialogInflater.inflate(R.layout.dialog_alert, null)
val alertDialog = AlertDialog.Builder(this).create()
alertDialog.setView(alertDialogView)
alertDialog.show()
}
}
Try this
override fun onSuccess() {
println("onSuccess called")
runOnUiThread { createDialog() }
}
Set view to dialog builder:
private fun createDialog() {
println("createDialog called")
val dialogInflater = LayoutInflater.from(this)
val alertDialogView = dialogInflater.inflate(R.layout.dialog_alert, null)
val alertDialog = AlertDialog.Builder(this).setView(alertDialogView).create()
alertDialog.show()
}
Related
My DialogFragment
private lateinit var listener: OnItemClickDialogListener
My interface
interface OnItemClickDialogListener{
fun onItemClickDialog(sortDesc: Boolean){
}
And my problem
listener = try {
targetFragment as OnItemClickDialogListener
}catch (e: TypeCastException){
throw TypeCastException()
}
targetFragment is deprecated and I don't know how to fix it. I spent a lot of time find solution.
My full code:
class SortDialogFragment: DialogFragment() {
private lateinit var listener: OnItemClickDialogListener
private lateinit var radioGroup: RadioGroup
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(requireContext())
val inflater = requireActivity().layoutInflater
val view: View = inflater.inflate(R.layout.fragment_dialog_sort, null)
radioGroup = view.findViewById(R.id.sort_RG)
builder.setView(view)
.setPositiveButton("OK") {di, i ->
when(radioGroup.checkedRadioButtonId){
R.id.sort_desc_RB -> { listener.onItemClickDialog(true) }
R.id.sort_asc_RB -> { listener.onItemClickDialog(false) }
}
}
.setNegativeButton("Cancel") {di, i -> }
return super.onCreateDialog(savedInstanceState)
}
override fun onAttach(context: Context) {
super.onAttach(context)
listener = try {
targetFragment as OnItemClickDialogListener
}catch (e: TypeCastException){
throw TypeCastException()
}
}
interface OnItemClickDialogListener{
fun onItemClickDialog(sortDesc: Boolean){
}
}
}
I just started learning Kotlin. I am using a custom ProgressDialog. Everytime I press back from the MainActivity, the app crashes with the following error:
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property dialog has not been initialized
at com.devApps.blog.CustomProgressDialog.getDialog(CustomProgressDialog.kt:17)
Here is my CustomProgressDialog :
private val progressDialognew = CustomProgressDialog()
lateinit var dialog: CustomDialog
fun show(context: Context): Dialog {
return show(context, null)
}
fun show(context: Context, title: CharSequence?): Dialog {
val inflater = (context as Activity).layoutInflater
val view = inflater.inflate(R.layout.progress_dialog_view, null)
if (title != null) {
view.cp_title.text = title
}
dialog = CustomDialog(context)
dialog.setContentView(view)
dialog.show()
return dialog
}
And this is my MainActivity code :
private val progressDialognew = CustomProgressDialog()
progressDialognew.show(this, "Optimizing Image...")
{
my tasks here
}
override fun onDestroy() {
super.onDestroy()
progressDialognew.dialog.dismiss()
}
UPDATE : I did as suggested now I am getting the same Error in the show() at the return dialog line. How to fix that ?
lateinit var dialog: CustomDialog
fun show(context: Context): Dialog {
return show(context, null)
}
fun show(context: Context, title: CharSequence?): Dialog {
if (::dialog.isInitialized) {
val inflater = (context as Activity).layoutInflater
val view = inflater.inflate(R.layout.progress_dialog_view, null)
if (title != null) {
view.cp_title.text = title
}
dialog = CustomDialog(context)
dialog.setContentView(view)
dialog.show()
}
return dialog
}
}
fun hideProgress() {
if (::dialog.isInitialized) {
if (dialog != null) {
dialog.dismiss()
}
}
Complete StackTrace :
kotlin.UninitializedPropertyAccessException: lateinit property dialog has not been initialized
at com.devApps.blog.CustomProgressDialog.show(CustomProgressDialog.kt:50)
at com.devApps.blog.PostActivity.upload(PostActivity.kt:328)
at com.devApps.blog.PostActivity.post(PostActivity.kt:320)
at com.devApps.blog.PostActivity.access$post(PostActivity.kt:54)
at com.devApps.blog.PostActivity$onCreate$5$2.onClick(PostActivity.kt:198)
If you close the activity without ever showing the dialog, this exception will happen. Because the dialog property in your CustomProgressDialog gets initialized only in show() method.
And onDestroy() method of the activity calls dismiss() on the dialog property that might not be initialized.
You can check if it's initialized first before dismissing it, consider adding this method inside CustomProgressDialog:
fun dismissDialog() {
if (::dialog.isInitialized) {
dialog.dismiss()
}
}
And call it from the activity:
override fun onDestroy() {
super.onDestroy()
progressDialognew.dismissDialog()
}
I'm trying to get my location using mapbox on an android application built with kotlin. I'm using the locationComponent method to get it, here is my code :
class PlaceholderFragment : Fragment(), OnMapReadyCallback, PermissionsListener {
private var permissionsManager: PermissionsManager = PermissionsManager(this)
private var mapboxMap: MapboxMap? = null
private var myCurrentLocation: LatLng? = null
var navigationMapRoute: NavigationMapRoute? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
pageViewModel = ViewModelProviders.of(this).get(PageViewModel::class.java).apply {
setIndex(arguments?.getInt(ARG_SECTION_NUMBER) ?: 1)
}
val theActivity = activity as NavigationActivity?
theSteps = theActivity?.theSteps
case = theActivity?.case
case = theActivity.case
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
activity?.let {
Mapbox.getInstance(
it,
getString(com.innoventiq.arkbeh.R.string.mapbox_access_token2)
)
}
val root =
inflater.inflate(com.innoventiq.arkbeh.R.layout.fragment_navigation, container, false)
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mapView.onCreate(savedInstanceState)
mapView.getMapAsync(this)
getMyLocation()
}
override fun onExplanationNeeded(permissionsToExplain: MutableList<String>?) {
Toast.makeText(
activity,
com.innoventiq.arkbeh.R.string.user_location_permission_explanation,
Toast.LENGTH_LONG
)
.show()
}
override fun onPermissionResult(granted: Boolean) {
if (granted) {
enableLocationComponent(mapboxMap?.style!!)
} else {
Toast.makeText(
activity,
com.innoventiq.arkbeh.R.string.user_location_permission_not_granted,
Toast.LENGTH_LONG
)
.show()
}
}
override fun onMapReady(mapboxMap: MapboxMap) {
this.mapboxMap = mapboxMap
mapboxMap.cameraPosition = CameraPosition.Builder()
.target(myCurrentLocation)
.zoom(14.0)
.build()
//mapView.setOnTouchListener { v, event -> true }
mapboxMap.setStyle(Style.OUTDOORS) {
enableLocationComponent(it)
}
}
#SuppressLint("MissingPermission")
private fun enableLocationComponent(loadedMapStyle: Style) {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(activity)) {
// Create and customize the LocationComponent's options
val customLocationComponentOptions = activity?.let {
LocationComponentOptions.builder(it)
.trackingGesturesManagement(true)
.accuracyColor(
ContextCompat.getColor(
activity!!,
com.innoventiq.arkbeh.R.color.colorGreen
)
)
.build()
}
val locationComponentActivationOptions =
activity?.let {
LocationComponentActivationOptions.builder(it, loadedMapStyle)
.locationComponentOptions(customLocationComponentOptions)
.build()
}
// Get an instance of the LocationComponent and then adjust its settings
mapboxMap?.locationComponent?.apply {
// Activate the LocationComponent with options
locationComponentActivationOptions?.let {
this?.activateLocationComponent(
it
)
}
// Enable to make the LocationComponent visible
isLocationComponentEnabled = true
// Set the LocationComponent's camera mode
cameraMode = CameraMode.TRACKING
// Set the LocationComponent's render mode
renderMode = RenderMode.COMPASS
}
} else {
permissionsManager = PermissionsManager(this)
permissionsManager.requestLocationPermissions(activity)
}
}
private fun getMyLocation() {
myCurrentLocation =
mapboxMap?.locationComponent?.lastKnownLocation?.latitude?.let {
mapboxMap!!.locationComponent.lastKnownLocation?.longitude?.let { it1 ->
LatLng(it,
it1
)
}
}
println("the location : $myCurrentLocation ")
}
override fun onStart() {
super.onStart()
mapView.onStart()
}
override fun onResume() {
super.onResume()
mapView.onResume()
}
override fun onPause() {
super.onPause()
mapView.onPause()
}
override fun onStop() {
super.onStop()
mapView.onStop()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView.onSaveInstanceState(outState)
}
override fun onDestroy() {
super.onDestroy()
mapView.onDestroy()
}
override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
}
companion object {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private const val ARG_SECTION_NUMBER = "section_number"
/**
* Returns a new instance of this fragment for the given section
* number.
*/
#JvmStatic
fun newInstance(sectionNumber: Int): PlaceholderFragment {
return PlaceholderFragment().apply {
arguments = Bundle().apply {
putInt(ARG_SECTION_NUMBER, sectionNumber)
}
}
}
}
}
I used almost the full fragment code just trying to give you a clear view of the steps I've used to get the location. When it comes to the line println("the location : $myCurrentLocation ")
inside the getMyLocation() function , it returns this output the location : null ,, any help on this ?
Note
When the map loads, it shows my location perfectly and tracks it, but I just can't get the LatLng of it.
I've found the answer, used LocationComponent.LocationEngine.getLastLocation callBack to get it , here is the code:
private fun getMyLocation() {
mapboxMap?.locationComponent?.locationEngine?.getLastLocation(object :
LocationEngineCallback<LocationEngineResult> {
override fun onSuccess(result: LocationEngineResult?) {
if (result != null) {
myCurrentLocation =
LatLng(result.locations[0].latitude, result.locations[0].longitude)
println("my location is : $myCurrentLocation")
getTheRoute(myCurrentLocation)
}
}
override fun onFailure(exception: Exception) {
toast(getString(R.string.failed_get_location))
}
})
}
I have OnClickInterface (with method fun onClickShape()) Main.class, and FlipFragment.class and ImageView (which called image in my code). My goal is make listener for image.
interface OnClickInterface {
fun onClickShape()
}
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initTabs()
var flip = FlipFragment()
flip.listener = object : OnClickInterface {
override fun onClickShape() {
Log.d("MainActivity", "Shape Pressed")
ToastUtils.showSuccessMessage(baseContext, "sometext")
}
}
}
fun initTabs() {
var adapter = TabsPagerFragmentAdapter(supportFragmentManager)
mViewPager.adapter = adapter
mTabLayout.setupWithViewPager(mViewPager)
}
}
onCreate in FlipFragment
var image = view.findViewById<ImageView>(R.id.fShapeView)
image.setOnClickListener(View.OnClickListener {
Log.d("FlipFragment", "PRESSED")
if (listener != null)
listener!!.onClickShape()
})
App was loading well, without errors. But when I pressed in the image I show in my log FlipFragment: PRESSED that's mean that my application call method from FragmentFlip, not override method from MainActivity. Why?
I searched error . My app show NPE here.
flip.listener = object : OnClickInterface {
override fun onClickShape() {
Log.d("MainActivity", "Shape Pressed")
ToastUtils.showSuccessMessage(baseContext, "someText")
}}
Why listener = null . I defined it with anonymous class.
All code in FlipFragment
class FlipFragment : Fragment() {
private var layout = R.layout.view_flip
var listener: OnClickInterface? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
var view: View
view = inflater!!.inflate(layout, container, false)
var image = view.findViewById<ImageView>(R.id.fShapeView)
image.setOnClickListener(View.OnClickListener {
Log.d("FlipFragment", "PRESSED")
if (listener != null){
listener!!.onClickShape()}
})
return view
}
companion object {
fun getInstanse(): FlipFragment {
var args = Bundle()
var flipFragment = FlipFragment()
flipFragment.arguments = args
return flipFragment
}
}
}
If you need all code it is FragmentPagerAdapter.class
class TabsPagerFragmentAdapter(fm: FragmentManager?) : FragmentPagerAdapter(fm) {
var tabs: Array<String> = arrayOf("Flip", "Multi")
override fun getItem(position: Int) = when(position){
0 -> FlipFragment.getInstanse()
1 -> Mulit.getInstanse() //it is empty now
else -> FlipFragment.getInstanse()
}
override fun getPageTitle(position: Int) = tabs[position]
override fun getCount() = tabs.size
}
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()