if i need to use a resource twice, is it better to store it in a String?
public class Testing extends Activity{
private String c_stringName;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
c_stringName= this.getString(R.string.name);
}
}
If you dig down to the machine. The only difference between them is 1 reference.
it may seem like running multiple
c_stringName= this.getString(R.string.name);
is doing alot of work but its is the same and storing it into a string reference.
Related
I was developing an exam score calculator app.
When I want to call AD methods,advertisements don't show up.
Calculation process happens in OnCreate method:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
/*Calculation...*/}
and other voids like:
public void requestAd() {
/*AD RQUESTING PROCESS...*/
}
and
public void showAd() {
/*AD SHOWING PROCESS...*/
}
AD team gave me this code to call the method and it works well:
requestButton.setOnClickListener(v -> requestAd());
showButton.setOnClickListener(v -> showAd());
But the Problem is I don't have buttons to call them so I tried this:
public class resultActivity extends AppCompatActivity {
public String responseId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
requestAd();
showAd();
/*Calculation...*/}
But when the activity starts ads don't show up!
The whole question is I want this methods to be called while this activity starts.
thank you.
Try building up the release version APK and test on it. Maybe your Ad-provider have some restrictions in debug version?
I made another class and moved request Ad and showAd there. Then, I made an object and called the method through object.
I have to mention that I changed a minor thing in requestAd but the main job was done by the object.
Thank You All.
Android Studio 3.4
I have the next activity:
public class CartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod()
}
private void someCommonMethod() {
// some code
}
}
Now I have 2 build types: debug and release.
In debug I add method someDebugMethod() to activity
public class CartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod()
someDebugMethod()
}
private void someCommonMethod() {
// some code
}
private void someDebugMethod() {
// some debug code
}
}
In release I add method someReleaseMethod() to activity
public class CartActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod()
someReleaseMethod()
}
private void someCommonMethod() {
// some code
}
private void someReleaseMethod() {
// some debug code
}
}
I read official documentation
and now project's stucture is:
app\src\debug\java\com\myproject\CartActivity.java
app\src\release\java\com\myproject\CartActivity.java
I remove CartActivity.java from app\src\main\java.
So, as a result, I have two files CartActivity.java.
Nice.
And now when I start the app in debug build type then runCartActivity in debug folder.
And when I start the app in release build type then runCartActivity in release folder.
Nice. It's work fine.
But suppose now I need to update the common method someCommonMethod(). This method used in both build types.
As a result, I need to update TWICE this method. First in app\src\debug\java\com\myproject\CartActivity.java and
then update same method with same code in app\src\release\java\com\myproject\CartActivity.java
So I think this is not good.
Because this is duplicate code. I need to copy & paste EVERY time in TWO files when update method someCommonMethod().
It's really bad.
How to avoid this duplicate code?
The ideal approach is when in CartActivity.java has only delta.
In app\src\debug\java\com\myproject\CartActivity.java has ONLY method someDebugMethod()
In app\src\release\java\com\myproject\CartActivity.java has ONLY method someReleaseMethod()
and common code is in app\src\main\java\com\myproject\CartActivity.java
Is it possible?
P.S. Suppose I have 3 build types.
As result, I need to update same code in three files. It's really not good.
Just create a CommonCartActivity in
app\src\main\java\com\myproject\CommonCartActivity.java
Then extend the class overriding the methods in the flavor implementations.
public class CartActivity extends CommonCartActivity {
protected void someCommonMethod() {
// some code
}
}
In this way CartActivity just inherits from CommonCartActivity with nothing else, duplicated in two flavors.
I think you do not need to complicate the structure of the project. Just use the real-time check.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
someCommonMethod();
if(BuildConfig.DEBUG){
someDebugMethod();
}else{
someReleaseMethod();
}
}
private void someCommonMethod() {
// some code
}
private void someDebugMethod() {
// some debug code
}
private void someReleaseMethod() {
// some release code
}
But, if you need to separate the code, try using one activity, but create a new class (for example, Fork) in two copies for release and debug. Create an instance of the class in the activity. In this class there will be a doMetnod() which will have the necessary code depending on the type of project. So you avoid duplication of the activity code.
I am trying to Print some messages using Log.i but it doesn't print anything, the problem starts after I updated the Android studio.
How can I solve the problem?
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("check","**************checking");
setContentView(R.layout.activity_main);
}
}
An alternate solution, but it's not the best.
System.out.print();
Make sure you select the device you are using, you can do it on the top left side of the Logcat.
for example if you used an emulator and than a device make sure the device is selected.
Try using the tag constant
public static final string TAG = "MainActivity"
Then do
Log.d(TAG, YOURMESSAGE)
Also as an alternative you can use System.out
I saw this some code :
....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected int getLayoutResource() {
return R.layout.activity_home;
}
....
In which they used getLayoutResource() instead of setContentView in order to add View Layout.
I don't understand, why it is so. Does getLayoutResource() before or alongside with onCreate(Bundle savedInstanceState).
I read some about getLayoutResource() (Gets the layout resource that will be shown as the View for this Preference.) in android developer document but i didn't understand .
Is there anyone to explain more.
I want to know where and where we should use getLayoutResource().
While I can't tell from your code snippet, I don't think that you are talking about Preference, since Preferences don't have onCreate (as far as I know). So, I am assuming that you are talking about a custom method named getLayoutResource in an activity. As such, getLayoutResource likely just returns the layout resource ID, without doing anything to it. The returned layout resource ID could then be used in onCreate by setContentView, which actually inflates the resource. We use it this way:
public abstract class BaseActivity {
#Override
onCreate(Bundle savedInstanceState) {
setContentView(getLayoutResource);
}
protected abstract int getLayoutResource();
}
public class MyActivity extends BaseActivity {
#Override
protected int getLayoutResource() {
return R.layout.my_activity_layout;
}
}
In short, you use getLayoutResource anywhere where you need the layout resource ID for a view.
To read more about setContentView, check out this: https://developer.android.com/reference/android/app/Activity.html#setContentView .
This answer discusses the difference between getLayoutResource and setContentView: What is the different between setContentView and getLayoutResource in android?
I was kind of stuck trying to pass the resources to a subclass used on my Activity. I solved it in two ways, but not sure if one or both will lead to possible memory leaks. So here is what I have so far:
-myactivity (the activity class)
-global (global class to the package, I'm using to to save global accesible variables)
-subclass (the subclass where I want to use a drawable resource)
a)
public class global{
public static Resources appRes;
....
}
public class myactivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
global.resApp = this.getResources();
...
}
private void somewhere(){
subclass tmp = new subclass();
tmp.subclasmethod();
}
}
public class subclass{
public subclass(){...}
public void subclassmethod(){
Bitmap bmp = BitmapFactory.decodeResource(Global.appRes, R.drawable.myres);
...
}
}
b)
public class myactivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
}
private void somewhere(){
subclass tmp = new subclass(this.getContext());
tmp.subclasmethod();
}
}
public class subclass{
Context context;
public subclass(Context context){
this.context = context
...
}
public void subclassmethod(){
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.myres);
...
}
}
Thanks in advance for you feedback.
If you want a global class to store application-wide values, you should at least not use your option a. Instead, take a look at the Application class, which is meant to help you with exactly this:
Base class for those who need to
maintain global application state.
Otherwise, the alternative you suggest in option b is an OK way to do it. At least if all you need is to pass along a reference to your application context so that you can access the resources.