Intent和SetContentView之间的区别 [英] Difference between an intent and setcontentview

查看:90
本文介绍了Intent和SetContentView之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的主要活动中,将视图加载为意图还是使用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);
    }
}

推荐答案

区别在于,使用第一种方法而不是创建新的Activity,您只是在更改当前Activity的布局.使用第二种方法,您将创建一个新的Activity.

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.

这篇关于Intent和SetContentView之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆