I use a AlertDialog with two spinners, the App itself is pretty fast, but when I start this Dialog the application slows down when the dialog is finished the speed rises.
One of the Spinners is really "heavy" it contains 60 elements consisting of one picture and one TextView. Picture (ImageView) is small, but the original picture is 512x512 PNG 12,54kB (is this to large?).
One ArralyList (for the small spinner) is initialized directly, the small one is initialized at start of the application.
This is the AlertDialog
public class dialog_CardWithdrawRequest extends AppCompatDialogFragment {
private static final String TAG = "dialog_CardWithdrawRequ";
private ArrayList<obj_PrintState> printStatesList;
private Spinner sp_DialogCardWithdrawRequest_Printer;
private Spinner sp_DialogCardWithdrawRequest_Country;
private sendChoosenCountryAndPrintState listener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (dialog_CardWithdrawRequest.sendChoosenCountryAndPrintState) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
" must implement sendChoosenCountryAndPrintState listener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
initArrays();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme);
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_cardwithdrawrequest, null);
final int Driver;
final int iCurrentDriverPosition;
Bundle receivingBundle = this.getArguments();
if(receivingBundle != null){
Driver = receivingBundle.getInt("Driver");
iCurrentDriverPosition = receivingBundle.getInt("CurrentCountryPosition");
}else{
Driver = INT_DEFAULT_VALUE;
iCurrentDriverPosition =0;
}
builder.setView(view)
.setTitle(R.string.dialog_CardWithdrawRequest_Title);
builder.setNegativeButton(R.string.Cancle, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
obj_Country selectedCountry = (obj_Country) sp_DialogCardWithdrawRequest_Country.getSelectedItem();
String sSelectedCountry = selectedCountry.getsHexCountryCode();
obj_PrintState selectedPrintState = (obj_PrintState) sp_DialogCardWithdrawRequest_Printer.getSelectedItem();
String sSelectedPrintRequest = selectedPrintState.getsPrinterRequestHex();
listener.choosenCountryAndPrintState(sSelectedCountry, sSelectedPrintRequest, Driver);
}
});
sp_DialogCardWithdrawRequest_Country = view.findViewById(R.id.sp_DialogCardWithdrawRequest_Country);
sp_DialogCardWithdrawRequest_Printer = view.findViewById(R.id.sp_DialogCardWithdrawRequest_Printer);
adapter_Spinner_Country adapterCountry = new adapter_Spinner_Country(getContext(), objCountryList);
adapter_Spinner_Printer adapterPrinter = new adapter_Spinner_Printer(getContext(), printStatesList);
sp_DialogCardWithdrawRequest_Country.setAdapter(adapterCountry);
sp_DialogCardWithdrawRequest_Printer.setAdapter(adapterPrinter);
sp_DialogCardWithdrawRequest_Country.setSelection(iCurrentDriverPosition);
return builder.create();
}
private void initArrays(){
printStatesList = new ArrayList<>();
printStatesList.add(new obj_PrintState(PRINTREQUEST_NONE, getText(R.string.dialog_CardWithdrawRequest_PrintRequest_None).toString()));
printStatesList.add(new obj_PrintState(PRINTREQUEST_UTC, getText(R.string.dialog_CardWithdrawRequest_PrintRequest_UTC).toString()));
printStatesList.add(new obj_PrintState(PRINTREQUEST_LOCAL, getText(R.string.dialog_CardWithdrawRequest_PrintRequest_Local).toString()));
}
public interface sendChoosenCountryAndPrintState{
void choosenCountryAndPrintState(String sSelectedCountry, String sSelectedPrintRequest, int Driver);
}
}
This is the call from mainActivity
helperThread = new HandlerThread("HelperThread");
helperThread.start();
Handler helperHandler = new Handler(helperThread.getLooper());
helperHandler.post(new Runnable() {
@Override
public void run() {
dialog_CardWithdrawRequest dialog_cardWithdrawRequest = new dialog_CardWithdrawRequest();
Bundle information = new Bundle();
information.putInt("Driver", iDriver);
information.putInt("CurrentCountryPosition", finalIPosition);
dialog_cardWithdrawRequest.setArguments(information);
dialog_cardWithdrawRequest.show(getSupportFragmentManager(),"CardWithdrawRequest");
}
});
As you can See, I already tried to post this "hard work" on a different thread, but I think an AlertDialog
always runs in UI thread, I don't know.
I also tried to use:
runOnUiThread(new Runnable() {
@Override
public void run() {
}
});