Reputation: 6191
While implementing Architecture components I am facing this issue
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleOwner
import android.os.Bundle
import com.reversebits.trendyvidz.R
class MainActivity : LifecycleOwner {
override fun getLifecycle(): Lifecycle {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme) //This shows error
setContentView(R.layout.activity_main) //This shows error
init()
}
private fun init() {
when {
isNetworkAvailable() -> toast("Yes Avail")
else -> toast("No")
}
}
}
How do I suppose to get Activity context-based methods here like setContentView()
of AppCompatActivity?
Upvotes: 1
Views: 1316
Reputation: 38243
As already pointed out, if you want to make an Activity you need to extend Activity.
Other than that there is a couple of issues in your code:
If you use support library 26.1.0+ and lifecycles 1.0.0-alpha9-1+ lifecycles are already included because
AppCompatActivity extends FragmentActivity
FragmentActivity extends SupportActivity
SupportActivity extends Activity implements LifecycleOwner
If you use older support libraries or lifecycles you had two options.
If your activity extended FragmentActivity
, you would instead extend LifecycleActivity
and that's it.
If you couldn't do that, you'd implement LifecycleRegistryOwner
, for example:
class MyActivity extends AppCompatActivity implements LifecycleRegistryOwner {
private final LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
@Override
public LifecycleRegistryOwner getLifecycle() {
return mLifecycleRegistry;
}
}
That's where sample codes end but I don't see any code actually dispatching the events. Upon investigating current SupportActivity
it turns out it's using ReportFragment
to dispatch all events. Add this to properly dispatch events:
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ReportFragment.injectIfNeededIn(this);
}
@CallSuper
protected void onSaveInstanceState(Bundle outState) {
this.mLifecycleRegistry.markState(State.CREATED);
super.onSaveInstanceState(outState);
}
Another thing, this is a mistake:
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate(savedInstanceState: Bundle?) {
The method onCreate
triggers ON_CREATE
event. Not the other way around. You'll get stack overflow error like this.
You use @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
annotation on methods, that you want to trigger automatically after onCreate
is called, not on onCreate
method itself.
Upvotes: 3
Reputation: 6191
Okay, I figured it out,
I just need to do this:
class MainActivity : AppCompatActivity(), LifecycleOwner {
As LifecycleOwner is just an interface having a single method
override fun getLifecycle(): Lifecycle {
TODO("Not implemented") //To change body of created functions use File | Settings | File Templates.
}
And I am able to annotate methods with this annotations after implementing LifecycleOwner.
@OnLifecycleEvent(Lifecycle.Event.ON_START) etc.
Upvotes: -1