如何在扩展 LifecycleActivity 的视图中设置 SupportActionBar
How to setSupportActionBar in a view that extends LifecycleActivity
-
android
-
android-activity
-
activity-lifecycle
-
android-architecture-components
-
android-architecture-lifecycle
我有一个扩展 AppCompactActivity 的 Activity,在 onCreate
方法中,我以通常的方式使用 setSupportActionBar
方法设置了 Toolbar
:
public class StepMasterActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step_master);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);`
}
}
但现在我有一个 ViewModel 组件并在作为此 activity 的子级的片段之间共享数据并管理生命周期我必须在 Activity 中获取此组件等等我使它延长生命周期Activity。
public class StepMasterActivity extends LifecycleActivity {
@Override
public class StepMasterActivity extends LifecycleActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step_master);
// setToolbar();
SharedViewModel sharedViewModel = ViewModelProviders.of(this).get(SharedViewModel.class);
}
}
但我注意到 LifecycleActivity 与 AppCompatActivity 无关,FragmentActivity 也无关。
public class LifecycleActivity extends FragmentActivity implements LifecycleRegistryOwner {
private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);
public LifecycleActivity() {
}
public LifecycleRegistry getLifecycle() {
return this.mRegistry;
}
}
我做错了什么吗?
更新 2017-10-05:LifecycleActivity
已被弃用。如果您使用 26.1.0
或更高的 support-fragment
和 appcompat-v7
,FragmentActivity
和 AppCompatActivity
都实现 LifecycleOwner
.
出于历史(也可能是歇斯底里的)目的,原始答案出现在下面。
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.
LifecycleActivity
绑定到 FragmentActivity
,而不是 AppCompatActivity
。
您应该能够创建自己的 AppCompatLifecycleActivity
,如下所示:
public class AppCompatLifecycleActivity extends AppCompatActivity implements LifecycleRegistryOwner {
private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);
@Override
public LifecycleRegistry getLifecycle() {
return mRegistry;
}
}
The most recent support library revision 26.1.0
将允许您使用 AppCompatActivity
Fragment and FragmentActivity (the base class for AppCompatActivity)
now implement the LifecycleOwner interface from Architecture
Components.
android
android-activity
activity-lifecycle
android-architecture-components
android-architecture-lifecycle
我有一个扩展 AppCompactActivity 的 Activity,在 onCreate
方法中,我以通常的方式使用 setSupportActionBar
方法设置了 Toolbar
:
public class StepMasterActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step_master);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);`
}
}
但现在我有一个 ViewModel 组件并在作为此 activity 的子级的片段之间共享数据并管理生命周期我必须在 Activity 中获取此组件等等我使它延长生命周期Activity。
public class StepMasterActivity extends LifecycleActivity {
@Override
public class StepMasterActivity extends LifecycleActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step_master);
// setToolbar();
SharedViewModel sharedViewModel = ViewModelProviders.of(this).get(SharedViewModel.class);
}
}
但我注意到 LifecycleActivity 与 AppCompatActivity 无关,FragmentActivity 也无关。
public class LifecycleActivity extends FragmentActivity implements LifecycleRegistryOwner {
private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);
public LifecycleActivity() {
}
public LifecycleRegistry getLifecycle() {
return this.mRegistry;
}
}
我做错了什么吗?
更新 2017-10-05:LifecycleActivity
已被弃用。如果您使用 26.1.0
或更高的 support-fragment
和 appcompat-v7
,FragmentActivity
和 AppCompatActivity
都实现 LifecycleOwner
.
出于历史(也可能是歇斯底里的)目的,原始答案出现在下面。
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.
LifecycleActivity
绑定到 FragmentActivity
,而不是 AppCompatActivity
。
您应该能够创建自己的 AppCompatLifecycleActivity
,如下所示:
public class AppCompatLifecycleActivity extends AppCompatActivity implements LifecycleRegistryOwner {
private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);
@Override
public LifecycleRegistry getLifecycle() {
return mRegistry;
}
}
The most recent support library revision 26.1.0
将允许您使用 AppCompatActivity
Fragment and FragmentActivity (the base class for AppCompatActivity) now implement the LifecycleOwner interface from Architecture Components.