简体   繁体   中英

How to change Views inside a Fragment from an onActivityResult that is inside another Activity?

This my look like a duplicated Question , but i have already tried some of the possible solutions given in other questions, after some time i decided that it would be best for me to make a new one. I am using Kotlin to make an Activity on Android that i use to host a Navigation Drawer and inside that activity there is a fragment with some buttons , when i click on the buttons i initiate a Scan for QrCodes

CamaraQrIntegrator = IntentIntegrator(activity!!)
CamaraQrIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE)
CamaraQrIntegrator.setPrompt("QrCodes")
CamaraQrIntegrator.setCameraId(0)
CamaraQrIntegrator.setBeepEnabled(false)
CamaraQrIntegrator.setOrientationLocked(false)
CamaraQrIntegrator.setBarcodeImageEnabled(false)
CamaraQrIntegrator.initiateScan()

Because ZXing requires me using the parent activity for the IntentIntegrator the onActivityResult is on an Activity instead of the Fragment i am using

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
/// I Try to access a progressbar inside the framgent 
}

This makes it so i cannot access any of the views inside the fragment , all return null when tried to called directly with with FindViewById , making a function inside the fragment code to access a view that was initialized after onCreateView, when i try to access the view it tells me the progressbar is null

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
publicView = inflater.inflate(R.layout.fragment_principal, container, false)
 progressBar = publicView!!.findViewById(R.id.progressBarPrincipal)
return publicView
}
fun setProgressBarVisible(){
progressBar.visibility = View.VISIBLE
}
fun setProgressBarGone(){
progressBar.visibility = View.GONE
}

when i access setProgressBarGone() on the activity it tells me lateinit ProgressBar has not been initializated , althought the fragment has not been destroyed and the progress bar was indeed initializated on OnCreateView.

val MainFragment:Fragment = MainFragment()
val fragment = MainFragment().setProgressBarVisible()

I have tried making the view on the fragment public, but still is returned as null , i have tried to make the progressbar public and still it is null for the activity, and this is not only happens when onActivity Result at any point i try to access a View in the fragment is return as null.

I don't know what to do , any help would be nice , please i really feel like thinking something from the beggining so , i would really appreciate new solutions over fixing , what i have. Still anything would be of help for me at this point.

在创建该 Fragment 对象并使用 craeted insatnace 在 onActivityResult 访问片段方法上获取片段实例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM