gpt4 book ai didi

java - 将 Activity 扩展到 AppCompatActivity

转载 作者:行者123 更新时间:2023-11-29 06:54:48 30 4
gpt4 key购买 nike

很抱歉,如果我的问题被重复,但我无法(或不知道如何谷歌)找到答案。我想使用 rxlifecycle-navi 库来将 rxjava 绑定(bind)到生命周期,所以我需要扩展 NaviActivity 类。问题是 NaviActivity 类扩展了 Activity,我需要为 toolbar 扩展 AppCompatActivity。如何在不尝试更改库源代码的情况下实现这种优雅?我找到了 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);
}}

最佳答案

简单的解决方案,在 com.trello.navi.component 包中创建您自己的 NaviAppCompatActivity

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);
}
}

引用:
https://github.com/trello/navi 上搜索“NaviAppCompatActivity”

关于java - 将 Activity 扩展到 AppCompatActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36573179/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com