Why do we set the content to onCreate (), and not to onStart () or onResume ()?

I created a small program to set the presentation of content in the onResume () method instead of onCreate () and its performance.

onResume()
{
        setContentView(R.layout.activity_main);
        editText1 = (EditText) findViewById(R.id.ed1);
        editText2 = (EditText) findViewById(R.id.ed2);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
+4
source share
3 answers

Since onCreate()Activity is called only once, this is the point at which most initialization should be performed: a call setContentView(int)to inflate the activity user interface, using findViewById to programmatically interact with widgets in the user interface, calling managedQuery(android.net.Uri , String[], String, String[], String)to extract cursors to display data, etc.

onResume() onStart() ( ), setContentView() .

+4

onResume serval times. onCreate - . log sth , .

+1

ContentView onStart onResume, . - . XML, , , .. , .

Activity onCreate . onResume onStart , .

+1

Source: https://habr.com/ru/post/1617835/


All Articles