我有一个扩展 AppCompactActivity 的 Activity,并且在onCreate
方法中我以通常的方式设置了Toolbar
usingsetSupportActionBar
方法:
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 中获取此组件,因此我将其扩展为 LifecycleActivity。
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;
}
}
难道我做错了什么?