19

I am using setContentView(R.layout.main) to switch the views in the same activity. I am calling some asynchronous task and populating the data on the main layout file after that I am changing the view by calling setContentView(R.layout.main) method.

I came to know that we should not use setContentView method multiple times for same activity. Though it is working fine for me.

Can anyone explain why we should not use setContentView method multiple times for the same activity to change the views?

Will it create any memory related exceptions? Could someone please clarify?

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
madan V
  • 844
  • 3
  • 19
  • 40
  • Not that I know it would cause any exceptions. (Except OutOfMemory Exception probably if you do this when Sys Memory is low.) Let's hear what other's says. – Calvin Mar 20 '13 at 09:35
  • Related: http://stackoverflow.com/q/6811989/783284 – Cody Mar 20 '13 at 09:41

3 Answers3

2

I think switching Views is not a good idea, because android platform already have strong framework to handle the transition in between the views and maintaining the state of each view associated with the Activity its always better to stick with the existing framework instead of thinking of some complex implementation that you have to go through to do all these things. If you do not need any of these things to taken care in your application and if only if you have only two or three screen in your entire application you can try switching the views. That even based on how your views are structured if you have complex logic and lot of data needed to create these views this wont be a good way of doing it.One more thing if you are adding more views say functionality to your application the load that need to be handled by the Activity will go high. In this case you will be declaring and initializing all views inside that particular Activity so maintaining all these views instances is heavy. If you want to know more about the Activty and Task kindly refer this link

Triode
  • 11,309
  • 2
  • 38
  • 48
  • 3
    In other words, changing the view will probably mean that you also change some logic and thus, a new activity is more suitable. – adrianp Mar 20 '13 at 09:45
  • i want to know after changing the view the memory allocated for previous view will be deallocated or not.and one more thing this process will cause any memory exceptions.can you clarify on these two issues.because i am using setContentView method to change the views. – madan V Mar 20 '13 at 09:47
  • are you getting memory exceptions? – the-ginger-geek Mar 20 '13 at 09:49
  • if you have any reference to that view inside your activity then it wont get cleared, the reference can be a subchild or even event listeners set for the view etc – Triode Mar 20 '13 at 09:50
  • as of now i am not getting any memory exceptions Neil – madan V Mar 20 '13 at 09:52
0

Well every time you call setContentView() you'll have to find all the layouts again besides that I think you "can" do it. But as discussed here this is ill adviced as it clearly goes against the android guidelines. Also Commonsware have some very important points here one of the most important being that you will be prone to leak memory as you forget to clean up stuff from your views etc. which Android normally would handle for you.

In short you should follow Android guidelines and use Fragments or start a new Activity.

Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155
0

According to the developer docs setContentView(int layoutResID) is used to

Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.

In best practice this method is used to Inflate your Activity layout on start up. This does not mean that it will cause issues in the future if you keep using this method. To quote a answer in this question

The setContentView on your Activity actually calls the setContentView on the Window used by the activity, which itself does a lot more than just inflating the layout.

I suggest that you find a alternative way to switch layouts like using a ViewPager with Fragments or some other Tabbing approach but in the end it all comes down to what you want to do.

This question might also give you what you're looking for.

Community
  • 1
  • 1
the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45