很抱歉,如果我的问题被公开,但我不能(或者不知道如何谷歌)找到答案。我想使用rxlifecycle-navi
库来将rxjava绑定到生命周期,所以我需要扩展NaviActivity
类。问题是NaviActivity
类扩展了Activity
,我需要为AppCompatActivity
扩展toolbar
。如何在不尝试更改库的源代码的情况下实现这种优雅?我发现article如何通过AppCompatDelegate
执行此操作,但看起来并不灵活。我认为这是一般问题,我不能扩展2个类。
我的BaseActivity
目前:
public abstract class BaseActivity extends NaviActivity implements AppCompatCallback {
private final ActivityLifecycleProvider provider = NaviLifecycle.createActivityLifecycleProvider(this);
public AppCompatDelegate delegate;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ButterKnife.bind(this);
delegate = AppCompatDelegate.create(this, this);
delegate.onCreate(savedInstanceState);
}
@Override
public void onSupportActionModeStarted(ActionMode mode) {
}
@Override
public void onSupportActionModeFinished(ActionMode mode) {
}
@Nullable
@Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}}
另一堂课:
public class CarWashesActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car_washes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
delegate.setSupportActionBar(toolbar);
delegate.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}}
答案 0 :(得分:4)
简单的解决方案,在NaviAppCompatActivity
包中创建自己的com.trello.navi.component
。
package com.trello.navi.component;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import com.trello.navi.Event;
import com.trello.navi.Listener;
import com.trello.navi.NaviComponent;
import com.trello.navi.internal.NaviEmitter;
public class NaviAppCompatActivity extends AppCompatActivity implements NaviComponent {
private final NaviEmitter base = NaviEmitter.createActivityEmitter();
@Override public boolean handlesEvents(Event... events) {
return base.handlesEvents(events);
}
@Override public <T> void addListener(Event<T> event, Listener<T> listener) {
base.addListener(event, listener);
}
@Override public <T> void removeListener(Listener<T> listener) {
base.removeListener(listener);
}
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
base.onCreate(savedInstanceState);
}
@Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
base.onCreate(savedInstanceState, persistentState);
}
@Override protected void onStart() {
super.onStart();
base.onStart();
}
@Override protected void onResume() {
super.onResume();
base.onResume();
}
@Override protected void onPause() {
base.onPause();
super.onPause();
}
@Override protected void onStop() {
base.onStop();
super.onStop();
}
@Override protected void onDestroy() {
base.onDestroy();
super.onDestroy();
}
@Override protected void onRestart() {
super.onRestart();
base.onRestart();
}
@Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
base.onSaveInstanceState(outState);
}
@Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
base.onSaveInstanceState(outState, outPersistentState);
}
@Override protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
base.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onRestoreInstanceState(savedInstanceState, persistentState);
base.onRestoreInstanceState(savedInstanceState, persistentState);
}
@Override protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
base.onNewIntent(intent);
}
@Override public void onBackPressed() {
super.onBackPressed();
base.onBackPressed();
}
@Override public void onAttachedToWindow() {
super.onAttachedToWindow();
base.onAttachedToWindow();
}
@Override public void onDetachedFromWindow() {
super.onDetachedFromWindow();
base.onDetachedFromWindow();
}
@Override public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
base.onConfigurationChanged(newConfig);
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
base.onActivityResult(requestCode, resultCode, data);
}
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
base.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
上搜索“NaviAppCompatActivity”
答案 1 :(得分:2)
创建AppCompatNaviActivity
扩展AppCompatActivity
。然后将NaviActivity
中提供的所有代码添加到AppCompatNaviActivity
。
现在,只要您需要扩展NaviActivity
,请展开AppCompatNaviActivity
,这将提供AppCompatActivity
和NaviActivity
的所有强大功能。
它有点破解,但希望这会帮助你。