Activities vs Views

I am working on an Android app that has multiple screens the user will need to navigate between and I am curious what the best practices are when switching between those screens. I am torn between creating a new Activity for each screen and simply changing the view (setContentView(R.layout.whatever)). The screens all share at least some variable values so I'm leaning toward changing views and using class level variables, but I'm worried a single activity could become very large and confusing with logic for multiple screens in a single file. I'd like to keep the code clean and separated, but I also don't want to be passing several variables around between views if that isn't needed.

Being new to Android development, I'm hoping some more experienced members of the community could share their thoughts and let me know how best to handle it.

Thanks!

Note: I wasn't planning on using a viewflipper. My thought was to use a button click event and then call setContentView() to a new view for the page I wanted to bring up next.

Example: My application starts up using R.layout.main as it's view. User clicks the Help button and it calls a method that runs setContentView(R.layout.help); to display the help screen as opposed to switching to a help activity.


You should use an activity per screen as this will make the best use of the framework and allow the OS to selectively kill off screens if things get tight.

If you have a single activity and resources get tight the OS has two choices; kill everything or kill nothing, and if the user is not using your app then it's most likely it'll kill everything.

If you use an Activity per screen the OS can kill off some of the screens the user hasn't visited for a while, whilst still allowing others to remain active which allows the user to go back to them quickly.

As for sharing variables and values, you could use the SQLite database or SharedPreferences stores for passing them around if they are widely shared, or use the putExtra methods in Intent if they're only of use from one screen to the next.


If you have variables that you will reuse make a base class for them, that you will extend.

This can be a your custom activity that extends Activity .

As far I can tell you have to create separate activities for each views, only a few situation can be handled by viewflippers.

链接地址: http://www.djcxy.com/p/68000.html

上一篇: Android活动和观看

下一篇: 活动与视图