Screen freezes when using a Yilmun on Android fragment on Create

Asked 2 years ago, Updated 2 years ago, 150 views

public void on Create (Bundle saved InstanceState) {

        super.onCreate(savedInstanceState);
        preferences = getActivity().getSharedPreferences(PreferencePutter.PREF_FILE_NAME, Activity.MODE_PRIVATE); 



        if (NetworkUtil.getConnectivityStatusBoolean((getActivity()))) {   
            //Determine network connection status

            request(); // request data from server

            while (!flag) { // exit when flag variable is true
            }
            flag = false; // reinitialize flag variable


        } } else {
            setLayoutWithout_Net(); // screen to recall when no network connection exists
        }

    }

Hello, I am posting questions from Monday. I've been working on the same problem for a few days. ㅜㅜ There was no problem with using it until the lower version of Lollipop, but the screen stops after logging in from the marshmallow This is happening.

The flag variable is boolean type and is used as a global variable, and the initialization state is false. After logging in, it seems that the screen stops due to the while part of the onCreate part of the screen.

In the above source, the while statement is on the condition that the server receives all the requested data and waits until it is received It's the part that I made.

When the data is received, the flag variable changes the value from the request function to True I didn't write it down here because I came out of the wilderness, but I'm going to show you the onCreateView below The values are then sprayed onto the screen.

There's a pause in the marshmallow version only in the release mode It is thought to be a screen stop due to infinite loops because it cannot escape from the while door.

If you delete the current while statement, the marshmallow will function normally, but after logging in, The price doesn't get on the screen right away.

It's not the sauce that I worked on from the beginning, so the explanation is not clear, so you can understand right away I don't know, but I don't mind if you give me a speculative answer, so if you know anything, please let me know I would be grateful.

Thank you for reading the long question. It's gotten a lot colder, so I hope you dress warmly and go to work.

android fragment oncreate

2022-09-22 15:05

1 Answers

The main thread (UI thread) should not be blocked until the server receives all the requested code. In other words, you should not use the while() statement in the main thread like the code you uploaded. You might think that it only occurs with marshmallow, but depending on your network environment, ANR can occur as much as you want.

First, remove the while() statement and modify the code by notifying the UI as an event (message) when the network request is completed. You can use AsyncTask or Thread + Handler as one of the methods. If you google it, you can easily find example codes.

AsyncTask

Thread, Handler


2022-09-22 15:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.