How to create a util that can be referenced multiple times in different activities

假如想象 提交于 2020-05-09 15:52:56

问题


I have successfully implemented a custom Dialog box that appears when the user tries to leave an activity via a back button or by using onBackPressed(). They can simply cancel the dialog box or continue, and leave the activity. This function has been implemented in multiple activities, however its making my code a lot longer than it needs to be. I wanted to know how to create a util that can be referenced in different activities, without the need for the chunk of code to copy pasted multiple times. Please note that I am retrieving the dialog title and description from string.xml

This is my code:

Dialog customDialog;
Button button_one, button_two;
TextView dialog_title, dialog_description;



customDialog = new Dialog(this);

//Back button will close app
@Override
public void onBackPressed() {
  customDialog.setContentView(R.layout.custom_dialog_box);

  dialog_title = customDialog.findViewById(R.id.dialog_title);
  dialog_title.setText(getString(R.string.leaving_activity_warning_title));
  
  dialog_description = customDialog.findViewById(R.id.dialog_description);  dialog_description.setText(getString(R.string.leaving_activity_warning_description));
  
  button_one = customDialog.findViewById(R.id.button_one);
  button_one.setText(getString(R.string.cancel));
  
  button_two = customDialog.findViewById(R.id.button_two);
  button_two.setText(getString(R.string.leave_anyway));

  button_one.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      customDialog.dismiss();
    }
  });

  button_two.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      customDialog.dismiss();
      finish();
      overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
    }
  });

  Objects.requireNonNull(customDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  customDialog.show();

}

UPDATE

  1. Created a Java file called "DialogBoxMessage"

DialogBoxMessage Code:

class DialogBoxMessage {

  private Dialog customDialog;
  private TextView dialog_title, dialog_description;
  private Button button_one, button_two;

  //Custom Dialog Box Initialization
  DialogBoxMessage(Button myButtonOne, TextView myDialogTitle, TextView myDialogDescription, Dialog myCustomDialog) {
    customDialog = myCustomDialog;
    button_one = myButtonOne;
    button_two = myButtonOne;
    dialog_title = myDialogTitle;
    dialog_description = myDialogDescription;
  }

  void leaveActivity() {

    customDialog.setContentView(R.layout.custom_dialog_box);

    dialog_title = customDialog.findViewById(R.id.dialog_title);
    dialog_title.setText(Resources.getSystem().getString(R.string.leaving_activity_warning_title));

    dialog_description = customDialog.findViewById(R.id.dialog_description);
    dialog_description.setText(Resources.getSystem().getString(R.string.leaving_activity_warning_description));

    button_one = customDialog.findViewById(R.id.button_one);
    button_one.setText(Resources.getSystem().getString(R.string.cancel));

    button_two = customDialog.findViewById(R.id.button_two);
    button_two.setText(Resources.getSystem().getString(R.string.leave_anyway));

    button_one.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        customDialog.dismiss();
      }
    });

    button_two.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        customDialog.dismiss();
      }
    });

    Objects.requireNonNull(customDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    customDialog.show();

  }
}
  1. I input the following code in another activity

Other activity code:

//Reusable exit dialog message
DialogBoxMessage dialogBoxMessage;


//Back button will close app
@Override
public void onBackPressed() {
  dialogBoxMessage.leaveActivity();
  finish();
}

But it doesn't seem to work, I think there are a lot of issues... please help :(


回答1:


I assume customDialog is a seperate class you wrote - therefore i would suggest you put main information like contentview, title, message or type in the constructor when you initialize ur Dialog. For your onClick Method I suggest you create an Interface to handle Button Clicks in your customDialog class.




回答2:


This could be implemented as a static method in a utilities class. The method would require 'this' as a parameter, which contains the activity context. The method should return the result of the button press. The activity can use this response to determine if finish() should be called or not.

UPDATE

I had suggested a simple static method, but you've gone down the object-oriented route. That's fine. However, your constructor requires passing in several views, which wouldn't appear to achieve the code efficiency you are after. Your constructor should just require the Activity context; everything else is encapsulated in your new class. In each Activity's onBackPressed method you will need to create the object with dialogBoxMessage = new DialogBoxMessage(this); before you can call any of that object's methods.



来源:https://stackoverflow.com/questions/61487134/how-to-create-a-util-that-can-be-referenced-multiple-times-in-different-activiti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!