Android Architecture components : Get Activity methods when using LifecycleOwner

1.3k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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:

1)

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

2)

If you use older support libraries or lifecycles you had two options.

2a)

If your activity extended FragmentActivity, you would instead extend LifecycleActivity and that's it.

2b)

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.

2
On

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.