简体   繁体   中英

Pass data from base activity to activity that extends it

I have an Activity that extend base Activity like so:

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        getInfo("Pojo")
        }
}

My base activity does some API access and returns the answer like so:

abstract class BaseActivity : AppCompatActivity() {

    companion object {
        @JvmStatic var compositeDisposable: CompositeDisposable?=null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        compositeDisposable = CompositeDisposable()
    }

    override fun onDestroy() {
        super.onDestroy()
        compositeDisposable?.clear()
    }

    fun getTvShows(query: String) {

        compositeDisposable?.add(
            ApiClient.getClient.getTV(Params.getParamsSearch(1, query))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this::handleResponseTvShows)
        )
    }

    private fun handleResponseTvShows(result: ObjectsSearchTVShows) {
       
       //Need to send result back to MainActivity or any activity that extends BaseActivity
    }
}

I need to send the result back to MainActivity or any activity that extends BaseActivity, how I can achive this?

Make handleResponseTvShows abstract and protected :

protected abstract fun handleResponseTvShows(result: ObjectsSearchTVShows)

and implement it in MainActivity :

override protected fun handleResponseTvShows(result: ObjectsSearchTVShows) {
  // process the result here
}

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