简体   繁体   中英

How to handle different initialized variables in layout and layout-landscape for android activity.java file?

Situation: In an Android Studio project in app/src/main/res you have the folders layout, and layout-land (landscape version of layout). Each of these has the folders has a file named activity_quiz.xml in them. You also have QuizActivity.java that uses activity_quiz.xml.

Layout has 4 variables that it initializes within its xml file. Layout-land has 3 variables that it initializes within its xml file. Layout-land does not have one of the variables that layout has. (You might not always want to use the exact same variables in portrait vs landscape)

QuizActivity.java sets listeners on all of the variables. However, because layout-land does not have a variable that is initialized in layout, the app will crash due to QuizActivity setting listeners on all of the variables (including the ones that are now not initialized).

In this situation, what is the best way to handle this? Should you have an if statement to check which type of layout the app is in, before setting listeners on each variable? If you do this, how do you check for what layout the app is in?

Or should you initialize all the variables through overriding onSaveInstanceState(Bundle)? (Or initialize the variables somewhere else?) -- This seems like bad coding practice.

I know some work arounds, but I wanted to know what would be considered the best coding standard to implement. Any help is appreciated!

Extra question: If for some reason the QuizActivity.java implementation for landscape mode is significantly different than portrait mode, what would be best in this situation? Keep all the code in one QuizActivity.java file with an if statement separating them, or separate activity files for each landscape and portrait?

Should you have an if statement to check which type of layout the app is in, before setting listeners on each variable? If you do this, how do you check for what layout the app is in?

Yes, that's a valid approach. Referencing this question , you could use the following snippet to check the orientation:

if(getResources().getConfiguration().orientation) == Configuration.ORIENTATION_LANDSCAPE){
    //In landscape
}else if(getResources().getConfiguration().orientation) == Configuration.ORIENTATION_PORTRAIT){
    //In portrait
}

Using this check in your onCreate method allows you to do different variable initialization depending on your screen orientation.

If for some reason the QuizActivity.java implementation for landscape mode is significantly different than portrait mode, what would be best in this situation? Keep all the code in one QuizActivity.java file with an if statement separating them, or separate activity files for each landscape and portrait?

I would say that separating the logic with an if/else statement would do. If your code tends to differentiate too much when toggling between landscape and portrait, I'd say that your design is flawed. Imo, only the graphical interface (your xml layout) should adapt to fit the screen when the orientation change, the user would be confused if there would be drastic changes to application behaviour just because of the orientation change.

You can use an if-statement and check the orientation in this way:

if (getResources().getConfiguration().orientation == Confirguration.ORIENTATION_LANDSCAPE){
   ...
} else {
   // it is ORIENTATION_PORTRAIT
}

http://developer.android.com/reference/android/content/res/Configuration.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM