stack
stack

Reputation: 653

Difference between an intent and setcontentview

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);
    }
}

Upvotes: 5

Views: 1982

Answers (2)

FoamyGuy
FoamyGuy

Reputation: 46856

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.

Upvotes: 7

Pierry
Pierry

Reputation: 989

Intent is for initialize new activity from activity...

setContentView is to set layout xml

Upvotes: 0

Related Questions