In Ice Cream Sandwich, when there's an Activity containing an EditText, the EditText will retain the Activity's Context even after the user leaves the Activity. To demonstrate this I've created TestLeakActivity, which allocates a large byte array. Since the Activity's Context is never garbage collected, the byte arrays accumulate on the heap, eventually causing an OutOfMemoryError. You can observe the heap growth by using the DDMS heap tool, and you can track the outstanding references to the EditText class by looking at the HPROF file in Eclipse MAT. To create memory leaks, go into LaunchActivity and just keep launching and backing out of TestLeakActivity.
LaunchActivity.java
package com.example.testleakproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class LaunchActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = new Button(this);
button.setText("Start TestLeakActivity");
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(LaunchActivity.this, TestLeakActivity.class);
startActivity(intent);
}
});
ViewGroup container = ((ViewGroup) findViewById(android.R.id.content));
container.addView(button);
}
}
TestLeakActivity.java
package com.example.testleakproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.EditText;
public class TestLeakActivity extends Activity {
private byte[] mSomeBytes = new byte[1048576];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditText editText = new EditText(this);
editText.setHint("TestLeakActivity");
ViewGroup container = ((ViewGroup) findViewById(android.R.id.content));
container.addView(editText);
}
}
This is a known bug, that will be fixed in ICS MR1.
This has not been fixed until now. (Android 4.2.1)
I've just spend several hours to find that I'm affected by this issue.
The issue seems to be caused by the spell checker. When I disable suggestions for the EditText view everything is properly garbage collected.
mInputType = mText.getInputType();
mText.setInputType(mInputType | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
I don't really want to disable this, since many users want spell checking. So, maybe there is a way to temporarily enable it when the input field receives the focus.
If you don't need the spell checker just add this to the EditText element in your layout xml instead:
android:inputType="textNoSuggestions"
That seems to fix it too.
Edit:
Just found this thread that appears to be related: Work around SpellCheckerSession leak?
I'm experiencing the same. My Gingerbread devices all work fine, but testing on my Galaxy Nexus this situation arises predictably. What your experiencing is likely why the MR1 and 4.0.3 updates rolled out so quickly.
You are running into the situation described in the Android resources section on memory leaks. See that page for some solutions as well.
I got the same problem,
I solved it by hiding the EditText ondismiss of my dialog.
mEditText.setVisibility(View.GONE);
Related
I am developing an Ionic 3 application, I recently published it and in production, where phones of all kinds and sizes were used, I noticed that there is an android native setting called "font-size" where you can make the size of texts in your phone bigger or smaller.
Some people (among others many older people) choose 'big' or even 'huge' text size. This unfortunately affects texts in my application on their phones and completely ruins the layout.
The way I am defining font sizes in my css files is with em values, but I also tried px.
Do you know if there is any way to prevent my application from adhering to this native android text-size setting? Or any other ways to fix it?
Please help,
Cheers
I found the solution! I used this phonegap plugin:
https://ionicframework.com/docs/v3/native/mobile-accessibility/
and used the method this.mobileAccessibility.usePreferredTextZoom(false);
This way, my app ignores the android font size settings!
I face similar issue. I have done as below for best practice :
this.platform.ready().then(()=>{
this.mobileAccessibility.getTextZoom().then((textZoom)=>{
if(textZoom>130){
this.mobileAccessibility.setTextZoom(130);
}
});
})
Setting text zoom with JS works with a jump from big to small size. But in MainActivity.java it applies immediately without visible delay
package io.ionic.starter;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.getcapacitor.BridgeActivity;
public class MainActivity extends BridgeActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setTextZoom(100);
}
}
The plugin suggested in the accepted answer was last updated in 2016. Instead of adding a new plugin, we can configure the webview settings to disable text zoom as discussed here.
So, inside MainActivity.java you can do something like this:
import com.getcapacitor.BridgeActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends BridgeActivity {
public void onResume() {
super.onResume();
WebSettings settings = bridge.getWebView().getSettings();
settings.setTextZoom(100);
settings.setSupportZoom(false);
}
}
On some of my Android projects I see that building got quite slow since using the new Jack compiler. I need it to use Java 8 features like lambdas.
But the long building time is a bit disturbing. So I set up a new Android project. It contains just a MainActivity with one button which reacts on your click.
package de.xappo.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
Toast.makeText(MainActivity.this, "Button clicked!", Toast.LENGTH_LONG).show();
}
});
}
}
There is nothing else except from the layout file which just contains a a TextView and a Button within a RelativeLayout.
As you can see in the picture all the jack gradle tasks together take about 75 seconds. Is this normal? This hole example app builds in less than 22 seconds without jack. So is this big difference normal?
I already managed Java heap size within my gradle.properties file:
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Is there still any thing to do to improve jack compiling performance? Or do you know about any updates planned on jack to improve building time?
As per this announcement, the Jack toolchain on Android is deprecated and java8 support will be directly integrated into Android's standard javac and dx toolchain. I switched to Jack for java8 support, but then transitioned to retrolambda because Jack was so slow.
I have been attempting to create a simple TabActivity with 3 Tabs. All works except if I put android:minSdkVersion="11" in the Manifest file, the icons are not shown. If I set `minSdkVersion="10", all is well.
I have looked high and low, but I have not been able to determine what is wrong.
I have put the same images in the seemingly appropriate resource directories:
res/drawable-hdpi-v5
res/drawable-ldpi-v5
res/drawable-mdpi-v5
res/drawable-xhdpi-v5
And the the code is simple:
import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
public class Review extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabs = getTabHost();
getLayoutInflater().inflate(R.layout.main,
tabs.getTabContentView(), true);
Resources resources=getResources();
Log.d("testing", String.format("icon: %d.%d",
resources.getDrawable(R.drawable.review).getIntrinsicWidth(),
resources.getDrawable(R.drawable.review).getIntrinsicHeight()));
TabHost.TabSpec details = tabs.newTabSpec("review"). setContent(R.id.review).
setIndicator(getString(R.string.review),
resources.getDrawable(R.drawable.review));
TabHost.TabSpec gallery=tabs.newTabSpec("gallery").setContent(R.id.photos)
.setIndicator(getString(R.string.gallery),
resources.getDrawable(R.drawable.photos));
TabHost.TabSpec reservation=tabs.newTabSpec("reservation").
setContent(R.id.reservation)
.setIndicator(getString(R.string.reservation),
resources.getDrawable(R.drawable.reservation));
tabs.addTab(details);
tabs.addTab(gallery);
tabs.addTab(reservation);
}
}
In digging into this, the only difference I can see internally under android 2.0 vs 3.0 is that Android uses a RelativeLayout instead of a LinearLayout in the 2.0 implementation.
Just to be certain that the icons images are being found, Log.d of above shows:
icon: 32.32 as it should.
Why does this shift from android 2.0 to 3.0 do this???? I am hopeful that someone else has run into this and it is obvious. Thanks very much for your help!
-- UPDATE:
I discovered today, as I looked more closely at what is actually happening when this code is built for android 3.0+, I learned that the ImageView's that come about when SetIndeicator(string, drawable) is called for each TabSpec, are actually never set and are actually NULL (ImageView.mDrawable==null) and INVISBLE.
If I force set those drawables to be set, and call ImageView.setVisiblity(View.VISIBLE) then they show up. However under android 2.0 they appear stacked with the image above and the text below as in:
<image>
<text>
Under android 3.0 they appear (when forced as above) side by side as in:
<image><text>
Thus it seems that things have changed a great deal and I need to investigate the changes for android 3.0 more carefully.
Stay tuned for more...
-- Final UPDATE:
Ultimately, I abandoned this avenue and decided that this style of doing things changed and is perhaps now depreciated and there are other better ways to do this and the icons are a bit old style.
strange, this solves the problem
//bmOptions.inSampleSize = 1;
//or better
bmOptions.inScaled = false;
more at:
https://stackoverflow.com/a/12088287/1320686
I am using gradient as background in my activity. on some android devices it doesn't look as good and smooth as in Photoshop, to fix this issue somebody told me use onAttachedToWindow() method.
I checked Android page (http://developer.android.com/reference/android/app/Activity.html#onAttachedToWindow()) and I found that this method is a part of android.app.Activityand I wrote following lines of code:
package com.test.test1;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Window;
public class Mainctivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
}
but when run the emulator, it crashed and in DDMS I saw this error:
11-25 10:48:13.353: E/dalvikvm(216): Could not find method android.app.Activity.onAttachedToWindow, referenced from method com.test.test1.MainActivity.onAttachedToWindow
What is my fault?
This method is available since API Level 5. What version of Android is running on the emulator?
As per the comments above, I've had this code tested on an actual device and it worked smoothly. So this is an emulator problem. Hopefully this will be resolved in later versions of the sdk.
Suddenly my buttons cause null pointer exceptions. I have not changed any code in this activity class. I have been up most of the weekend trying to pin this down. I get null pointer exceptions in both the emulator and on device.
This is a simple screen with buttons. I could easily scrap this and write new code, but would like to know what has caused this issue. If not, I will just write new code.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Button b1 = (Button) findViewById(R.id.MainActBtn);
b1.setOnClickListener(new View.OnClickListener() { //Error is here
public void onClick(View v) {
Intent intent = new Intent(StartScreen.this, MainActivity.class);
startActivity(intent);
}
});
Any help?
Try a fresh clean and build of your project. I got this error too in the past, caused by a changed R.class Resource class, but the static references in the using classes weren't properly replaced by the incremental java compiler, so the resources are no longer found.
I think You are not getting Null Pointer Exception because of the Button click just because of the declaration of the button..
You can check before writting the onClickListener..
if(b1==null)
Log.i("Null","Null");
The Problem is may be in setting the XML view or also check the button you are using is defined in the same xml you are using in setContentView.
After this all, also Clean and Build your project.