5

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

2 Answers 2

7

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.

1
  • That makes perfect sense. In my case it would make more sense to call it as an intent since the back button should take them back to the page before not show the "desktop". Thanks
    – stack
    Commented Dec 16, 2012 at 3:35
0

Intent is for initialize new activity from activity...

setContentView is to set layout xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.