I wonder why Android Fragment is slow in the initial call. [Material door]

Asked 2 years ago, Updated 2 years ago, 68 views



public class a {

   private static int[] idLayoutMenuButtonNumber = {
            R.id.idButton_a,
            R.id.idButton_b,
            R.id.idButton_c,
            R.id.idButton_d,
            R.id.idButton_e,
    };
    public static Button[] idLayoutMenuButton = new Button[idLayoutMenuButtonNumber.length];

    public static   Menu_a            menu_a;
    public static   Menu_b             menu_b;
    public static   Menu_c              menu_c;
    public static   Menu_d             menu_d;
    public static   Menu_f           menu_f;

    public static Menu_Base menuSystemInstances[] = null;


    int selectedMainMenuIndex,

    final static int MAIN_MENU_NONE             = -1;
    final static int MAIN_MENU_a          = 0;
    final static int MAIN_MENU_b           = 1;
    final static int MAIN_MENU_c            = 2;
    final static int MAIN_MENU_d           = 3;
    final static int MAIN_MENU_e         = 4;
    final static int MAIN_MENU_f         = 5;   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MenuButtonDefine(idLayoutMenuButton, idLayoutMenuButtonNumber);

        menu_a            =       new Menu_a();
        menu_b             =       new Menu_b();
        menu_c              =       new Menu_c();
        menu_d             =       new Menu_d();
        menu_e           =       new Menu_e();
        menu_f         =       new Menu_f();


        menuSystemInstances = new Menu_Base[] {
                menu_a,
                menu_b,
                menu_c,
                menu_d,
                menu_e,
                menu_f,
        };

        SYSTEM_NENU_MAIN_NUM = menuSystemInstances.length;
        selectedMainMenuIndex = MAIN_MENU_NONE;

    }

     public void MenuButtonDefine(final Button[] idLayoutMenuButton, int[] idLayoutMenuButtonNumber) {
        idImageButton_Exit = findViewById(R.id.idImageButton_Exit);
        idImageButton_Exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _Adpater_Menu.setVisibility(View.GONE);
                selectedMainMenuIndex = MAIN_MENU_NONE;
            }
        });

        for(int i = 0 ; i < idLayoutMenuButton.length ; i++ ) {
            idLayoutMenuButton[i] = findViewById(idLayoutMenuButtonNumber[i]);
            idLayoutMenuButton[i].setOnClickListener(this);
            idLayoutMenuButton[i].setOnTouchListener(this);
        }
    }

public void selectSubMenu(int index){
        String subMenuTitleName[] = { "a", "b", "c", "d", "e", "f" };

        idLayoutMenuButton[selectedMainMenuCursor].setSelected(false);
        selectedMainMenuCursor = index;
        idLayoutMenuButton[selectedMainMenuCursor].setSelected(true);

        selectedMainMenuIndex = index;

        idLayoutTOP_TITLEName.setText(subMenuTitleName[selectedMainMenuIndex]);

        idLayoutMenuButton[saveMenuPosition].setBackgroundColor(Color.TRANSPARENT);
        idLayoutMenuButton[saveMenuPosition].setTextColor(Color.WHITE);

        switch(selectedMainMenuIndex){
            case MAIN_MENU_a          :
                saveMenuPosition = MAIN_MENU_a;
                getFragmentManager().beginTransaction().replace(R.id.idLayoutCNETER_SettingName, menu_a).addToBackStack(null).commit(); break;
            case MAIN_MENU_b           :
                saveMenuPosition = MAIN_MENU_b;
                getFragmentManager().beginTransaction().replace(R.id.idLayoutCNETER_SettingName, menu_b).addToBackStack(null).commit(); break;
            case MAIN_MENU_c            :
                saveMenuPosition = MAIN_MENU_c;
                getFragmentManager().beginTransaction().replace(R.id.idLayoutCNETER_SettingName, menu_c).addToBackStack(null).commit(); break;
            case MAIN_MENU_d           :
                saveMenuPosition = MAIN_MENU_d;
                getFragmentManager().beginTransaction().replace(R.id.idLayoutCNETER_SettingName, menu_d).addToBackStack(null).commit(); break;
            case MAIN_MENU_e         :
                saveMenuPosition = MAIN_MENU_e;
                getFragmentManager().beginTransaction().replace(R.id.idLayoutCNETER_SettingName, menu_e).addToBackStack(null).commit(); break;
            case MAIN_MENU_f       :
                saveMenuPosition = MAIN_MENU_f;
                getFragmentManager().beginTransaction().replace(R.id.idLayoutCNETER_SettingName, menu_f).addToBackStack(null).commit(); break;
        }
    }
}

The code for invoking the current Fragment is as above. It's exactly that format.

It doesn't say how to call him There are about 6 buttons in the code I'm showing you In fact, there are about 18 buttons that connect different flags.

If you press one of the hard keys, the menu screen is VIS processed in GONE, and then you can use the buttons inside Invoke each fragment.

When I asked you a question last time, someone told me to look at the heavy thread on the main page, but I don't think it's a problem because it's not that bad, although the thread of the difficult work is spinning.

The problem with delay only on the first call, There is no delay at all on the second invocation of the same itemten thousand, The first time a different item is called, the delay is severe.

android fragment

2022-09-22 14:52

1 Answers

I think there are two things that can be doubted.

The thread of a difficult task is circulating, but I don't think it's a problem because it's not that bad.

It's a problem that we can't judge because we're not cpu.

Calling commit() does not immediately execute the transaction. Rather, it's more like scheduling this transaction to occur in the activity's UI thread ("default" thread) as soon as the thread is ready. However, if necessary, call executePendingTransactions() in the UI thread to execute the transactions submitted by commit() immediately. You don't have to do this if the transaction is not dependent on the operation of other threads.

This is where you will be guided in the Official Document Guide. Try using the above method.

The second is that the fragment itself takes a lot of initialization work from onCreate to onResume. I'd appreciate it if you could bring that part and update the question.


2022-09-22 14:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.