开发者

Android setContentView or Intents?

I have a very simple 2 screen android app.

Is there any downside to simply switching out the layouts via setContentView or should i be using 开发者_JAVA百科intents?Don't want to bugger up my app if something is wrong with this.


Another thing to consider is that activities form a stack. If you want to be able to go back to the previous activity via the 'back' button, then you need to use activity. But if it is something simple like a 'loading' screen when your app starts and you don't have to go back to it again, setting content view would be a much better idea.


Well as stated on Android Dev http://developer.android.com/reference/android/content/Intent.html

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Therefore if your two screens are 2 different applications I would say you want to simply use setContentView.

it will simplify your code when you want to pass info from one to the other views


There is nothing wrong with having two views in a single activity. This approach is more light-weight, as you don't need to go through the phase of stopping one activity and then starting another one. However, it will make your activity code bulkier. Consider now if you are going to need more functionality or more views in the future and if the answer is yes, then it would be better to create separate activities.


If the view is light-weight (a bunch of text boxes), then it should not matter. On the other hand, if the two screens are largely independent and heavy, you could use two different activities. The primary advantages with this approach are:

  • If there is an error in the second screen (an activity in this case), your application will fall back to the first screen whereas in the case of using the view, the whole application crashes
  • Better readability
  • Easier to add more functionality in the future
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜