简体   繁体   English

单身人士班,有效利用资源在不同活动中敬酒

[英]Singleton class that show a toast in different activities using resources efficiently

I have a singleton class (let's call it: SingleWorker ) that it's used in multiple activities. 我有一个单例类(我们称其为: SingleWorker ), 该类在多个活动中使用。 Depending on the case it shows a toast or a progress dialog if a task is being done. 根据情况,如果正在执行任务,它会显示祝酒词或进度对话框。 I have 2 ways in my mind on how to do this: 我有两种方法可以做到这一点:

1) creating a separate method in the singleton - SingleWorker, that will get the context of the activity before the toast/progress is shown 1)在单例中创建一个单独的方法-SingleWorker,该方法将在显示吐司/进度之前获取活动的上下文

2) in the extended Application singleton, I will store, every time I enter a new activity (and on its onResume) its instance, that i will then use that in the SingleWorker singleton. 2)在扩展的应用程序单例中,每次我输入一个新活动(及其onResume上)它的实例时,我都会存储该实例,然后在SingleWorker单例中使用它。

Which one is the better choice (I guess the first one), can I do it better? 哪个是更好的选择(我想是第一个),我可以做得更好吗? I tried to find an efficient answer by myself, but I need a direction or a tip. 我试图自己找到一个有效的答案,但是我需要一个方向或技巧。 Any idea, suggestion, advice or link is gratefully received. 任何想法,建议,建议或链接都​​将受到感激。 Thanks for reading ! 谢谢阅读 !

I was using the second approach for a long time and decided to move to the first one after a few problens. 我长时间使用第二种方法,并在遇到一些问题后决定改用第一种方法。 Storing the context in the BaseActivity is better when you need the it outside the context (a background thread for example) To show toast's or dialog's you can just pass it to the method in singleton class ( approach 1), you will be free of memory leaks and will not have to worry about giving the contextStored a new value in onResume() and onDestroy() methods. 当您需要将上下文存储在上下文之外时,最好将其存储在BaseActivity中(例如,后台线程)。要显示吐司或对话框,只需将其传递给单例类中的方法(方法1),您将没有内存。泄漏并且不必担心在onResume()和onDestroy()方法中为contextStored提供新值。

Here is an example of a method to show a dialog: 这是显示对话框的方法的示例:

 public static void showAcceptDialog(String message, final Context contex, View.OnClickListener onClickYes, View.OnClickListener onClickNo) {
    dialog = new Dialog(contex);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.setContentView(R.layout.layout_of_your_dialog);
    TextView tv = (TextView) dialog.findViewById(R.id.generic_accept_message);
    //Seta o titulo do dialogo
    tv.setText(message);
    Button btNo = (Button) dialog.findViewById(R.id.generic_dialog_no);
    if (onClickNo != null) {
        btNo.setOnClickListener(onClickNo);
    } else{
        btNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    Button btYes = (Button) dialog.findViewById(R.id.generic_dialog_yes);
    if(onClickYes != null){
        // dialog.dismiss();
        btYes.setOnClickListener(onClickYes);
    }
    else {
        btYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
    dialog.show();
}

Toast s are not bound to a specific Activity in terms of their visibility. Toast就可见性而言,不限于特定的Activity That's one of the reasons why they where somewhat 'deprecated' (not officially, that's just my interpretation of Google's design guidelines and their own apps) in favor of the Snackbar which is bound to a specific View . 这就是为什么他们偏'“弃用”(不是正式的,这只是我对Google的设计指南和他们自己的应用的解释),而赞成绑定到特定ViewSnackbar的原因之一。

I see no advantage in using an activity context instead of the application context to create a Toast . 我看不到使用activity context而不是application context来创建Toast

Vice versa, when letting your Singleton hold a reference to activity context s, you might very easily create a memory leak by holding on to an already destroyed Activity . 反之亦然,当让您的Singleton持有对activity context s的引用时,您可能会很容易通过坚持已经被销毁的Activity来造成内存泄漏。 You could just pass in the Activity as a method parameter, but that might not be so convenient (eg calling that method from a class other than Activity ). 您可以只将Activity作为方法参数传入,但这可能不那么方便(例如,从Activity以外的类中调用该方法)。

Long story short: I would suggest to let your SingleWorker have a reference to the application context to create the Toast . 长话短说:我建议让您的SingleWorker引用application context来创建Toast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM