Updating state to the same value directly in the component body during render causes infinite loop

TL;DR The first example is an unintentional side-effect and will trigger rerenders unconditionally while the second is an intentional side-effect and allows the React component lifecycle to function as expected. Answer I think you are conflating the “Render phase” of the component lifecycle when React invokes the component’s render method to compute the diff for … Read more

Jetpack Compose Navigation loads screen infinitely

I re-implemented your posted code with 2 screens, HomeScreen and SettingScreen and stripped out some part of the UiState class and its usages. The issue is in your HomeScreen composable, not in the StateFlow emission. You have this mutableState val uiState by viewModel.uiState.collectAsStateWithLifecycle( initialValue = UiState.Speak ) that is being observed in one of your … Read more

Is while(1); undefined behavior in C?

It is well defined behavior. In C11 a new clause 6.8.5 ad 6 has been added An iteration statement whose controlling expression is not a constant expression,156) that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a … Read more

Java: Infinite Loop Convention [closed]

There is no difference in bytecode between while(true) and for(;;) but I prefer while(true) since it is less confusing (especially for someone new to Java). You can check it with this code example void test1(){ for (;;){ System.out.println(“hello”); } } void test2(){ while(true){ System.out.println(“world”); } } When you use command javap -c ClassWithThoseMethods you will … Read more

Updating state to the same state directly in the component body

TL;DR The first example is an unintentional side-effect and will trigger rerenders unconditionally while the second is an intentional side-effect and allows the React component lifecycle to function as expected. Answer I think you are conflating the “Render phase” of the component lifecycle when React invokes the component’s render method to compute the diff for … Read more

Validate the type of input in a do-while loop

The problem is that “scanf()” can leave unread data in your input buffer. Hence the “infinite loop”. Another issue is that you should validate the return value from scanf(). If you expect one integer value … and scanf returns “0” items read … then you know something went wrong. Here is an example: #include <stdio.h> … Read more

How can I change the EditText text without triggering the Text Watcher?

Short answer You can check which View currently has the focus to distinguish between user and program triggered events. EditText myEditText = (EditText) findViewById(R.id.myEditText); myEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (myEditText.hasFocus()) { // is only executed if the EditText was directly changed by the user … Read more