2016-07-04 41 views
2

我的应用程序中有我的自定义对话框构建器,它将触发许多事件,如:Asynctasks,Web服务消息,UI输入错误或来自没有活动上下文的服务。我经常在我的Application类中创建一个名为currentActivity的活动。然后在每个活动的简历上他们坐在currentActivity我的应用程序中全局对话框的最佳做法是什么

@Override 
    protected void onResume() { 
     super.onResume(); 
     MyApplication.currentActivity = MainActivity.this; 

然后在创建对话框的情况下,我使用该上下文。但我有一个问题。例如我打开RegisterActivity然后currentActivity改为它。然后当应用程序转到背景或某些其他活动打开的情况下,我的应用程序将崩溃创建与该上下文的对话框。所以处理女巫活动是currentActivity是一件令人头疼的事情。我搜索了一下,发现有些人在非布局活动中嵌入了CustomDialog,然后打开该活动。但似乎并没有很好的解决方案。

修订

比如我有一个处理我的所有发送短信一个SMSManager类。我想打开对话框,用户在发送短信之前选择了一些自定义选项。

那么在我的整个应用程序中,全局对话框的最佳实践是什么?

回答

4

首先 - 这是一个非常不好的做法,将其保存到引用活动(或一般Context)。 Android始终为您提供对可用于创建对话框的Context对象的引用。在Activity它是对象本身(this),在Fragment您可以访问Context通过getActivity()getContext(),在View - getContext()

如果您需要显示自定义班级中的对话框,并且没有参考Context,请确保您使用上述方法为班级提供Context引用。

不要尝试显示Service中的任何对话框 - 在显示任何对话框之前,确保您的应用程序处于前景和可见状态。您可以使用事件总线(或LocalBroadcastManager)将状态(错误,消息或其他)发送到当前可见的ActivityFragment。在这种情况下,“当前可见的活动或片段”仅仅是正在监听这种事件的ActivityFragment。开始收听onStart()并停止收听您的ActivityFragmentonStop()方法。如果您不想依赖任何正在运行的活动来显示对话框,并且不想等待用户下次启动应用程序时,我会建议使用notifications而不是对话框。

给出一个Context等。无论您希望使用一个辅助对话框生成器类这样你就可以创建自定义对话框:

public class DialogBuilder { 
    private String title; 
    private String message; 
    private String primaryButtonTitle; 
    private String secondaryButtonTitle; 
    private Dialog.OnClickListener primaryButtonListener; 
    private Dialog.OnClickListener secondaryButtonListener; 
    private Activity context; 
    private boolean showIcon; 
    private boolean cancellable; 

    public DialogBuilder(Activity context) { 
     this.context = context; 
    } 

    public DialogBuilder setTitle(@StringRes int title) { 
     this.title = context.getString(title); 
     return this; 
    } 

    public DialogBuilder setTitle(String title) { 
     this.title = title; 
     return this; 
    } 

    public DialogBuilder setMessage(@StringRes int message) { 
     this.message = context.getString(message); 
     return this; 
    } 

    public DialogBuilder setMessage(String message) { 
     this.message = message; 
     return this; 
    } 

    public DialogBuilder setShowIcon() { 
     showIcon = true; 
     return this; 
    } 

    public DialogBuilder setPrimaryButton(@StringRes int title, Dialog.OnClickListener listener) { 
     primaryButtonTitle = context.getString(title); 
     primaryButtonListener = listener; 
     return this; 
    } 

    public DialogBuilder setSecondaryButton(@StringRes int title, Dialog.OnClickListener listener) { 
     secondaryButtonTitle = context.getString(title); 
     secondaryButtonListener = listener; 
     return this; 
    } 

    public DialogBuilder setCancellable(boolean cancellable) { 
     this.cancellable = cancellable; 
     return this; 
    } 

    public AlertDialog create() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     View dialogView = LayoutInflater.from(context).inflate(R.layout.my_custom_dialog, null);   
     builder.setView(dialogView); 

     // get your custom views here and configure them based on given settings (field values of this class) 

     final AlertDialog dialog = builder.create(); 
     return dialog; 
    } 
} 

例如使用(在Fragment):

new DialogBuilder(getActivity()) 
    .setTitle(R.string.title) 
    .setMessage(R.string.message) 
    .setPrimaryButton(R.string.ok, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // do something 
      dialog.dismiss();   
     } 
    }) 
    .setSecondaryButton(R.string.settings_cancel, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // do something 
      dialog.dismiss();   
     } 
    }).create().show(); 
+0

我知道保持currentActivity并不好,我寻找更好的解决方案。我的自定义对话框生成器与你的非常相似,但它的上下文点。正如我所说的问题,我想从许多类中打开对话框,其中一些没有任何活动上下文。例如我想从我的SMS管理器类或与应用程序上下文一起使用的Web服务类中显示对话框。所以我不能用他们的上下文打开对话框。 – Kenji

+0

我更新了我的答案,提示通知 – artkoenig

+0

,所以你说不显示任何服务的UI对话框,只是播放一些事件并捕获活动。这听起来不错,并与MVC架构相匹配。但如果我的服务想与用户沟通并提供一些意见呢? – Kenji

-1

创建一个自定义对话框类是这样的:

public class DialogBoardOut extends Dialog { 

private TextView txtBoardOut; 
private RelativeLayout rel_close_boardout; 

public DialogBoardOut(Context mContext) { 
    super(mContext); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dialog_boardout); 

} }

调用,比如:

DialogBoardOut dialogQrCode = new DialogBoardOut(HomeBaseActivity.this); 
      dialogQrCode.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      // dialogQrCode.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
      dialogQrCode.show(); 
      dialogQrCode.getWindow().setLayout(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); 
+0

我的问题是而不是使用自定义的Dialog构建器。这是关于上下文 – Kenji

+0

如果你想使用片段中的对话框,然后使用getActivity()。getApplicationContext()和在活动发送getApplicationContext() 请详细说明你没有上下文的条件..所以我会指导你 –

相关问题