我有用于显示Dialog的AlertDisplay类,我需要使用匕首类在此类内进行活动上下文。
ApplicationComponent.java
@Singleton
@Component(modules = {AndroidSupportInjectionModule.class, ApplicationModule.class, ActivityBuilder.class, FragmentBuilder.class, ViewModelBuilder.class, NetworkModule.class, ServiceBuilder.class})
public interface ApplicationComponent extends AndroidInjector<AircraftOnLoadApplication> {
void inject(AircraftOnLoadApplication app);
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
@BindsInstance
Builder baseUrl(@BaseUrl String baseUrl);
ApplicationComponent build();
}
}
ApplicationModule.jva
@Module
public class ApplicationModule {
@Provides
@Singleton
Context provideContext(Application application) {
return application;
}
}
ActivityBuilder.java
@Module
public abstract class ActivityBuilder {
@ContributesAndroidInjector
abstract LoginActivity contributeLoginActivityInjector();
}
这就是我在oncreate方法中注入application.java类的方式。
daggerApplicationComponent = DaggerApplicationComponent.builder()
.application(this)
.baseUrl(BuildConfig.baseURL).build();
daggerApplicationComponent.inject(this);
下面是我的警报显示类,我在其中使用静态活动引用来显示非活动类的对话框,为此我希望在此类中创建活动上下文。
public class AlertDisplay {
private final Context context;
private final INavigationService navigationService;
private Dialog progressDialog;
private CustomProgressDataBinding binding;
@Inject
public AlertDisplay(Context context, INavigationService navigationService) {
this.context = context;
this.navigationService = navigationService;
}
@Override
public void displayAlert(String title, String message) {
if (this.context == null) {
return;
}
Dialog dialog = new Dialog(BaseActivity.currentActivity);
dialog.requestWindowFeature(1);
dialog.setContentView(R.layout.dialog_alert);
TextView titleTextView = dialog.findViewById(R.id.textViewHeader);
TextView messageTextView = dialog.findViewById(R.id.textViewMessage);
Button positiveButton = dialog.findViewById(R.id.buttonPositive);
messageTextView.setText(message);
titleTextView.setText(title);
if (!dialog.isShowing()) {
dialog.show();
}
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
positiveButton.setOnClickListener(v -> {
dialog.dismiss();
});
}
}
预先感谢您的帮助