I'm using conductor
and I want to initialize GoogleApiClient
in it.
As it is told in conductor
doc, Conductor is made for replacing a Fragment
. In Fragment
I can easily get an activity
. Is there a way do to that in conductor
?
class SignInController : Controller(), GoogleApiClient.OnConnectionFailedListener {
private lateinit var mGoogleApiClient: GoogleApiClient
private lateinit var mFirebaseAuth: FirebaseAuth
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
val view = inflater.inflate(R.layout.controller_sign_in, container, false)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(view.context.getString(R.string.default_web_client_id))
.requestEmail()
.build()
mGoogleApiClient = GoogleApiClient.Builder(view.context)
.enableAutoManage(view.context /* I need an activity here */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build()
mFirebaseAuth = FirebaseAuth.getInstance()
return view
}
}
I tried to use a view.context
instead of activity
but it doesn't work. It requires only FragmentActivty
.
How to initialize GoogleApiClient
inside conductor or to pass it there?
I wouldn't like to use DI
here.
I found the solution of this issue: It's very simple. The view.context is already the MyActivity and I just need to cast it:
GoogleApiClient.Builder(view.context).enableAutoManage(view.context as MyActivity, this)
You also can get parent activity
just by calling getActivity()
in your controller