I have a base class ActivityA that has some dialogs:
public static final int SOME_DIALOG_ID = 1;
public static final int OTHER_DIALOG_ID = 2;
ActivityB inherits ActivityA and comes with its own dialogs:
public static final int MY_DIALOG_ID = 1;
public static final int CONFLICT_DIALOG_ID = 2;
As you can see, the dialog IDs are no longer unique within the activity. This is a problem that I keep running into. Easy to fix, but tedious to maintian. There must be some good design pattern for this particular issue. Please, enlighten me!
Q: What is the best way to organize dialog IDs?
In my situation, I create class called AppConstant.java that contain all constant in current application, some of it:
public static final int INTENT_REQUEST_CODE_ACT_MAIN_BROWSE_PICTURE = 1;
public static final int INTENT_REQUEST_CODE_ACT_MAIN_CAMMERA_SNAP = 2;
public static final int INTENT_REQUEST_CODE_ACT_PROCESS_IMAGE_PROCESS = 3;
public static final int INTENT_REQUEST_CODE_ACT_PROCESS_BROWSE_PICTURE_FOR_BACK_DATA = 4;
Using this convention, I can easily maintain my ID within single class.
How about use 1, 2 for ActivityA, then 11, 12 for inherited class, then 101, 102 (or 21, 22) for further inheritance depth.
Related
#StringDef, #IntDef etc has made life much simpler and good alternatives to using enums.
Typically we see that these are defined as follows:
#IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
#Retention(RetentionPolicy.SOURCE)
public #interface NavigationMode {}
public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2;
....
Would there be any issue if one was to define these inside of the annotation itself. eg:
#IntDef({NavigationMode.NAVIGATION_MODE_STANDARD, NavigationMode.NAVIGATION_MODE_LIST, NavigationMode.NAVIGATION_MODE_TABS})
#Retention(RetentionPolicy.SOURCE)
public #interface NavigationMode {
int NAVIGATION_MODE_STANDARD = 0;
int NAVIGATION_MODE_LIST = 1;
int NAVIGATION_MODE_TABS = 2;
}
....
This comes in handy when these def annotation is defined separately so it could be used across modules.
I am using #IntDef notoriously. It's awesome, especially since Android Studio 2.0 can work correctly with this annotation and switch statement.
I am usually using this pattern:
public static final class NavigationMode {
#IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
#Retention(RetentionPolicy.SOURCE)
public #interface Value {
}
public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2;
// Suppress default constructor for noninstantiability
private NavigationMode() {
throw new AssertionError();
}
}
Then I can refer to the value through NavigationMode.NAVIGATION_MODE_STANDARD and the annotation is #NavigationMode.Value with a bit simpler #IntDef definition (without NavigationMode. prefixes).
But I must say... I like your second approach really much!
#IntDef({NavigationMode.NAVIGATION_MODE_STANDARD, NavigationMode.NAVIGATION_MODE_LIST, NavigationMode.NAVIGATION_MODE_TABS})
#Retention(RetentionPolicy.SOURCE)
public #interface NavigationMode {
int NAVIGATION_MODE_STANDARD = 0;
int NAVIGATION_MODE_LIST = 1;
int NAVIGATION_MODE_TABS = 2;
}
I might even switch to it. Although I'm not a big fan of the way Android Studio colors it then:
I don't think there should be any issues with values defined this way.
It's my first use of Android studio. But when I use it to create my first project, I found that in R.java(The location is app/build/source/r/debug/com.example.myapplication)
The beginning is like this:
public final class R {
public static final class anim {
public static final int abc_fade_in = 0x7f040000;
public static final int abc_fade_out = 0x7f040001;
public static final int abc_slide_in_bottom = 0x7f040002;
public static final int abc_slide_in_top = 0x7f040003;
public static final int abc_slide_out_bottom = 0x7f040004;
public static final int abc_slide_out_top = 0x7f040005;
}
public static final class attr {
public static final int actionBarDivider = 0x7f01005a;
public static final int actionBarItemBackground = 0x7f01005b;
public static final int actionBarPopupTheme = 0x7f010054;
public static final int actionBarSize = 0x7f010059;
public static final int actionBarSplitStyle = 0x7f010056;
public static final int actionBarStyle = 0x7f010055;
public static final int actionBarTabBarStyle = 0x7f010050;
public static final int actionBarTabStyle = 0x7f01004f;
public static final int actionBarTabTextStyle = 0x7f010051;
public static final int actionBarTheme = 0x7f010057;
public static final int actionBarWidgetTheme = 0x7f010058;
public static final int actionButtonStyle = 0x7f010072;
public static final int actionDropDownStyle = 0x7f01006d;
public static final int actionLayout = 0x7f01002c;
public static final int actionMenuTextAppearance = 0x7f01005c;
public static final int actionMenuTextColor = 0x7f01005d;
public static final int actionModeBackground = 0x7f010060;
public static final int actionModeCloseButtonStyle = 0x7f01005f;
public static final int actionModeCloseDrawable = 0x7f010062;
It list so many items that if I put them to Word, they will fill 38 pages in 11 font size. I tried deleted them, rebuilt them, restart, create another project, but they still exist. I had used Eclipse so I know how it should looks like. What's the problem? Is it caused by Gradle or Android Studio itself?
Deleting, rebuilding, restarting are not the correct approaches you should take.
These lines are generated by the Android Support Library.
Including the Support Libraries in your Android project is considered a best practice for application developers, depending on
the range of platform versions your app is targeting and the APIs that
it uses. Using the features the libraries provide can help you improve
the look of your application, increase performance and broaden the
reach of your application to more users. If you use the Android code
template tools, you will notice that all the Android application
templates include one or more of the Support Libraries by default.
[1] http://developer.android.com/tools/support-library/index.html
Yet, I don't understand why you want to take them off.
R.java is an auto-genrated file by android AAPT(Android Asset Packaging Tool). It has some significance, for every resource in the res folder a corresponding static integer field is created automatically.
So irrespective of the number of times you delete it, it will be generated again.
If you want to reduce the number of lines in the R.java file try deleting some values in the res folder.
The more layout files, widgets and images and other resources you have, the length of the R.java file is going to increase.
I hope this answers your doubts.
Whenever I design an Android Activity that is called with startActivityForResult I always find myself blurting out any number than comes to mind and using it as the request code.
Is there a specified pattern or numbering scheme I should follow for these request codes?
(ps: Should I move this to a different Stack Exchange page?)
It depends on you, I use this pattern, example:
class MainActivity extends Activity {
public static final int REQUEST_CODE_PHOTO = 1;
public static final int REQUEST_CODE_CROPPER = 2;
//code
}
in java i know this code is a good programming practice,
but i read some article there are good programming practice that
is bad for Android, i just want to know if this type of code
can affect the Aplication Performance issue when it comes to android programing?
for example
public class Main {
static int age = getAge(10); /***************** THIS LINE */
public static void main(String[] args) {
System.out.println(age);
}
private static int getAge(int i) { /***************** THIS METHOD */
i = i + 1;
return i;
}
}
This situation seems perfectly fine and wouldn't effect performance.
I personally would be careful with this practice though, you could potentially call a method dependant on variables that are yet to be initialized.
public class Main {
static int age = getDogYears(10);
int dogRatio; // dogRatio is not yet initialized
public static void main(String[] args) {
System.out.println(age);
dogRatio = 7;
}
private static int getDogyears(int i) {
i = i * dogRatio; // null pointer exception because dogRatio is not initialized
return i;
}
}
If the problem is really this simple though I would also ask why you wouldn't just make age = 11;
You're not going to see any noticeable performance hits, and I can't see anything wrong with the code, but it could be improved.
This method is a lot easier to read and cleans up the code a little:
private static int getAge(int i){
return i++;
}
You also then have to wonder why you even need it. As you're passing it an integer and not the value of a variable, why not just initialise it without the method call?
static int age = 11;
What i need to do is create an app which will generate a random mathmatical expression for the user to solve based on a difficulty level that they select.
eg. novice: a random operation on 2 terms
easy: random operations on 2 or 3 terms
What I'm struggling with is creating a class to handle the creation of expressions
My Game class is as follows:
package w1279057.CW1;
import android.app.Activity;
import android.os.Bundle;
public class Game extends Activity {
public static final String KEY_DIFFICULTY = "w1279057.CW1.difficulty";
public static final int DIFFICULTY_NOVICE = 0;
public static final int DIFFICULTY_EASY = 1;
public static final int DIFFICULTY_MEDIUM = 2;
public static final int DIFFICULTY_GURU = 3;
private int puzzle[];
#Override
public void onCreate(Bundle savedInstanceState) {
int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE);
setContentView(R.layout.gameview);
}
}
What i think i need is a class which has methods for generating a specific amount of operations randomly and then generating the random numbers for the expressions as well, once i have a valid expression i need to update a textview to display the expressions.
Am i on the right track?
I think this could be a good approach, as you will decouple the mathematical logic from your Activity. So... my advice is to go in this way :)