Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between an intent and setcontentview

Tags:

android

In my main activity is there a difference between loading a view as an intent or using setContentView?

public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
}

Or is this better? Not sure what the differnce is if they both load the layout file?

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         Intent i = new Intent(MainActivity.this, CalculateTip.class);
         startActivity(i);
    }
}
like image 677
stack Avatar asked Dec 16 '12 03:12

stack


People also ask

What is setContentView?

SetContentView is used to fill the window with the UI provided from layout file incase of setContentView(R. layout. somae_file). Here layoutfile is inflated to view and added to the Activity context(Window).

What is the different between setContentView () function and startActivity () function?

So, with respect to time, setContentView alone is faster, since it doesn't start a new activity. Hence, your app will show the new screen faster... On the other hand, if you call startActivity, this activity is put on the stack, so you can go back by pressing the back button.

Why do we need to call setContentView () in onCreate () of activity class?

onCreate() method calls the setContentView() method to set the view corresponding to the activity. By default in any android application, setContentView point to activity_main. xml file, which is the layout file corresponding to MainActivity.

What is the purpose of super onCreate () in android?

Q 9 – What is the purpose of super. onCreate() in android? The super. onCreate() will create the graphical window for subclasses and place at onCreate() method.


1 Answers

The difference is that with the first way you are not creating a new Activity, you are simply changing the layout of the current Activity. With the second way, you are creating a new Activity.

The practical difference will be that with the second way after you've started the new Activity you can press the back button and be taken back to the first. Whereas with the first way once the second layout is shown if you pressed the back button it would finish the current (only) activity which would put the user back to whatever they were doing before entering your application.

Which is "better" is impossible to determine without knowing more about what specifically you are trying to accomplish.

like image 160
FoamyGuy Avatar answered Oct 07 '22 03:10

FoamyGuy