使用 android 架构组件实现侧边栏导航

Implementing a sidebar nav using android architecture components

使用 android 的新架构组件,您不能使用 android 提供的默认自动生成的 class 导航抽屉 class...为什么?因为它从 AppCompatActivity 扩展为工具栏等小部件提供 support.v7 lib 的使用。现在使用新架构组件 Lifecycleactivity 而不是扩展 AppCompatActivity 时,您无法实现默认导航抽屉 class - 任何人都可以提供给我一个解决方法或一个如何做到这一点的例子?

来自Lifecycle documentation

Note: Since the Architecture Components are in alpha stage, Fragment and AppCompatActivity classes cannot implement it (because we cannot add a dependency from a stable component to an unstable API). Until Lifecycle is stable, LifecycleActivity and LifecycleFragment classes are provided for convenience. After the Lifecycles project is released, support library fragments and activities will implement the LifecycleOwner interface; LifecycleActivity and LifecycleFragment will be deprecated at that time.

他们继续提供 instructions for implementing a LifecycleOwner,这样您就可以避免使用 LifecycleActivity:

public class MyActivity extends AppCompatActivity
    implements LifecycleRegistryOwner {
  LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);

  @Override
  public LifecycleRegistry getLifecycle() {
    return lifecycleRegistry;
  }
}