androidkotlinconductor

Call activity method from conductor


I have an Activity with a zsmb:materialdrawer and conductor

class MenuActivity : AppCompatActivity(), GoogleApiClient.OnConnectionFailedListener{

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_menu)
        router = Conductor.attachRouter(this, container, savedInstanceState)
        if (!router.hasRootController()){
            router.setRoot(RouterTransaction.with(HomeController()))
        }
    }

    fun addDrawerItem() {
        // not implemented
    }

    private fun initViews(savedInstanceState: Bundle?) {
        toolbarMenu.setTitle(R.string.menu_title)

        drawerResult = drawer {

            primaryItem(getString(R.string.public_chats)){
                identifier = 101
                icon = R.drawable.abc_ic_star_black_48dp
                onClick(pushController(ChatRoomsController()))
            }
            primaryItem(getString(R.string.private_chats)){
                identifier = 102
                icon = R.drawable.abc_ic_star_black_48dp
                onClick(openActivity(ChatListActivity::class))
            }

        }
    }

}

class HomeController: Controller() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
        mView = inflater.inflate(R.layout.controller_sign_in, container, false)
        val signInWithGoogle = mView.findViewById<Button>(R.id.sign_in_with_google)
        signInWithGoogle?.setOnClickListener({
            // handle action with drawer here
            // mView.addDrawerItem()
        })
        return mView
    }
}

And I want to call my Activity method from the conductor. I'm trying to execute mView.addDrawerItem() but this method is not available there. As I know conductor is a replacement for a fragment and in fragment I can easily call activity method through the interface. What is the best practice to do it in conductor?


Solution

  • Conductor Controllers have a getActivity() method just like Fragments do. You'd do it the same whether you're using a Controller or a Fragment.