2

I have heard things about how it is bad to use setContentView()

Pattern "One activity, multiple views": Advantages and disadvantages

However I was wondering, would it be unlikely that my application will cause memory leaks, if I use setContentView() once in the onResume() method of my activity?

Whenever the user opens my app, it checks to see if something has been enabled in settings. If it has been enabled then the app uses a different screen compared to the original screen.

Therefor my code looks like this:

 @Override
    protected void onResume() {
       super.onResume();
       InputMethodManager im = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
       String list = im.getEnabledInputMethodList().toString();
       if(Stuff is true){
        setContentView(R.layout.activityscreen_enabled);
       }
   }

}

Would using setContentView() be unlikely to cause memory leaks and other such problems? Or is there a better solution?

Community
  • 1
  • 1
Foobar
  • 7,458
  • 16
  • 81
  • 161

2 Answers2

0

I'm doing Android since few years now and I have never done that because I like to stick to the pattern which is almost always having the setContentView in the onCreate.

However, I do not believe that you would have big troubles doing that (for the memory leaks I mean).

Nevertheless, I do not see the point of doing such a thing, the pattern of the Activity (or how I understood it) is more:

I create a view in the onCreate and I update its data in the onResume and if the data are A then add/remove this view and if the data are B add/remove this other view.

To be complete, I read your (really good) link and I think you maybe misunderstood how you can apply what Commonsware is saying: you can have multiply views without having different setContentView: your view structure needs, in this case, to be really modular and you will be able to load all the subviews dynamically (or, at least, it's how my colleague and I are doing ;) ).

For your example, I would have an empty layout for the base of the activity (let's say a blue background) and then for every view I want to have (every case), I would have a dynamic layout that I load at some point in the life cycle (probably at onResume). I do not believe that what you're doing is particularly bad but I doubt that it was thought like this ^^

This link agrees with me

Community
  • 1
  • 1
Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57
0

If you need multiple screens use a Fragment or even create a new Activity inside of messing around with the view for some reasons

  • It's not good to have single Activity for the whole app or it will be so long and complicated.
  • Your onResume() would need to handle the new views and their ids, onClickListeners... etc.
  • onResume() is called many times unlike onCreate() so it would be a waste of time and memory to load the views over and over.
  • According to android doc in activity life cycle about onPause() and onResume()

Because this state can transition often, the code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait.

SaNtoRiaN
  • 2,212
  • 2
  • 15
  • 25