Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we set content view in onCreate() and not in onStart() or onResume()?

Tags:

android

I've created a small program to set the content view on onResume() method instead of onCreate() and its working fine.

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();
            }
        });
    }
}
like image 387
aspan88 Avatar asked Nov 28 '15 16:11

aspan88


People also ask

What is the difference between onCreate () and onStart?

Android App Development for Beginners onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.

Why is onResume called after onCreate?

onResume() will never be called before onCreate() . Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() . onResume() will also be called the first time, when the activity has been created, right?

What is the relationship between onStart () and onResume ()?

onStart() called when the activity is becoming visible to the user. onResume() called when the activity will start interacting with the user. You may want to do different things in this cases.

What is onCreate method in Android Studio?

onCreate is used to start an activity. super is used to call the parent class constructor. setContentView is used to set the xml.


3 Answers

You don't setContentView in onStart or onResume because it's inefficient. Setting up the layout is a pretty heavyweight task. You have to parse the XML, create the Views, measurements, drawing, etc. It's the sort of task that you want to run at most once.

During the lifetime of your Activity class onCreate gets called exactly once. onResume and onStart will get called multiple times without your Activity getting destroyed.

like image 113
asadmshah Avatar answered Oct 20 '22 22:10

asadmshah


As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String) to retrieve cursors for data being displayed, etc.

It is inefficient to set the content in onResume() or onStart() (which are called multiple times) as the setContentView() is a heavy operation.

like image 36
Jainendra Avatar answered Oct 20 '22 22:10

Jainendra


onResume you may call it serval times. onCreate is just one time. Try log sth in they and skip to other activity than back.

like image 1
tiny sunlight Avatar answered Oct 20 '22 22:10

tiny sunlight