App lags with android:layerType="software" - android

I am making a really simple app (basically a listview with only a few images). It generally performs really well on my testing device (a Motorola Moto G with the stock android that comes with it).
However, I want my ListView to have dotted dividers. Therefore I made a list_divider.xml file defining the divider and set this to be the divider to be used in the ListView. I have tried that out on my Moto G, but it didn't display the line dotted.
So I've googled around and found here a solution to the problem. Disabling hw acceleration for the view solved the error and displayed the divider correctly.
However, now I have another problem: Since disabling hw acceleration the app began to lag when scrolling.
Is there a solution to having a dotted divider and not disabling hw acceleration or at least have the performance nearly as good as before disabling it?
Edit: Here is the adapter code I use:
public class AstronautArrayAdapter extends ArrayAdapter<Astronaut> {
public static final String TAG = "AstronautArrayAdapter";
public AstronautArrayAdapter(Context context, List<Astronaut> objects) {
super(context, R.layout.astronauts_list_row, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RowViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
convertView = inflater.inflate(R.layout.astronauts_list_row, parent, false);
holder = new RowViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.nameOfAustronaut);
holder.daysInSpace = (TextView) convertView.findViewById(R.id.numberOfDaysInSpace);
holder.country = (ImageView) convertView.findViewById(R.id.countryImage);
holder.title = (TextView) convertView.findViewById(R.id.titleOfAstronaut);
convertView.setTag(holder);
} else {
holder = (RowViewHolder) convertView.getTag();
}
Astronaut astronaut = getItem(position);
holder.name.setText(astronaut.getName());
holder.daysInSpace.setText(Integer.toString(astronaut.getDaysInSpace()));
holder.country.setImageBitmap(astronaut.getCountryImage(getContext()));
holder.title.setText(astronaut.getTitle());
return convertView;
}
private static class RowViewHolder {
TextView name;
TextView daysInSpace;
ImageView country;
TextView title;
}
}
Edit 2: Here are the two layout files in question:
activity_main.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/astronautsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="16dp"
android:dividerHeight="3dp"
android:divider="#drawable/list_divider"
android:layerType="software" />
list_divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:width="1px"
android:color="#android:color/black"
android:dashWidth="5px"
android:dashGap="1px" />
<size android:height="3dp" />
</shape>

possible answer for the lag you're seeing is:
add this line inside the dependencies section to your build.gradle
compile 'com.squareup.picasso:picasso:2.3.2'
and replace this line
holder.country.setImageBitmap(astronaut.getCountryImage(getContext()));
with
Picasso.with(context).load(/* insert the URL here */).into(holder.country);
and remove the code that is downloading all the images and saving locally. Picasso automagically download/decode the bitmaps and manage disk and ram cache for you.
ps.:
if you're not using Gradle:
You should change to Android-Studio and use Gradle
forget what I said about adding the line to the gradle file and just download the jar file from their website, put into the libs folder and make sure to mess on the project settings to make sure that jar goes inside your APK (I honestly do not remember how those settings work because I abandoned Eclipse long time ago)

As you did not mentioned it, have you implemented the ViewHolder pattern?
As the ListView recycles views it has to inflate a layout and insert any text and images in. You normally call findViewById in this section of code which is quite expensive. The ViewHolder pattern solves this and is easy to implement.
See here for a great tutorial by Google
http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Related

How do I layout my React Native Fire TV SubtitleView correctly?

I am working on a React Native implementation of the Bitmovin player using their Android SDK. At this stage, I'm not sure how specific this is to the Bitmovin player, but as they don't officially support React Native at this stage, I want to ask about this on SO first. This is a React Native UI Component with a custom view, using a layout file. I am trying to present a subtitle view on top of a player view, and I have based my layout on Bitmovin's simple examples. In fact I have simplified the layout even further:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.bitmovin.player.PlayerView
android:id="#+id/bitmovinPlayerView"
app:shutter_background_color="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bootsplash_background">
<com.bitmovin.player.SubtitleView
android:id="#+id/bitmovinSubtitleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foregroundGravity="center" />
</com.bitmovin.player.PlayerView>
</LinearLayout>
This presents the SubtitleView at the top of the screen. Nothing I have tried so far presents the SubtitleView at the bottom of the screen in the more common position. I have experimented with every single parameter on all of these elements, as far as I can tell. Here is the code that initialises the view:
public void init() {
inflate(context, R.layout.player_container, this);
StyleConfig styleConfig = new StyleConfig();
styleConfig.setUiEnabled(false);
PlayerConfig playerConfig = new PlayerConfig();
playerConfig.setStyleConfig(styleConfig);
playerView = findViewById(R.id.bitmovinPlayerView);
player = Player.create(context, playerConfig);
playerView.setPlayer(player);
player.on(SourceEvent.Loaded.class, this::onLoad);
player.on(PlayerEvent.Playing.class, this::onPlay);
player.on(PlayerEvent.Paused.class, this::onPause);
player.on(PlayerEvent.Seek.class, this::onSeek);
player.on(PlayerEvent.TimeChanged.class, this::onTimeChanged);
player.on(PlayerEvent.Destroy.class, this::onDestroy);
player.on(PlayerEvent.Seeked.class, this::onSeeked);
player.on(PlayerEvent.PlaybackFinished.class, this::onPlaybackFinished);
player.on(PlayerEvent.Ready.class, this::onReady);
player.on(SourceEvent.Error.class, this::onError);
player.on(SourceEvent.SubtitleChanged.class, this::onSubtitleChanged);
player.on(PlayerEvent.Error.class, this::onError);
subtitleView = findViewById(R.id.bitmovinSubtitleView);
subtitleView.setPlayer(player);
player.setVolume(100);
}
I have read that React Native styles the top-level view of a UI Component, so this is my only clue at this stage. I'm unsure how to respond to that info however...
EDIT: The problem is likely to be that dynamically updating view layouts in Android in React Native is not straightforward. This has been discussed at length here.
EDIT 2: I have tried to listen for global layout changes, which is one of the proposed workarounds for view layout issues:
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
requestLayout();
}
});
This is called as expected, but has no effect on the subtitleView, which still displays at the top of the player, which seems to be because it has a height of 0.
EDIT 3: Another suggested solution that didn't work for me:
private void setupLayoutHack() {
Choreographer.getInstance().postFrameCallback(new Choreographer.FrameCallback() {
#Override
public void doFrame(long frameTimeNanos) {
manuallyLayoutChildren();
getViewTreeObserver().dispatchOnGlobalLayout();
Choreographer.getInstance().postFrameCallback(this);
}
});
}
private void manuallyLayoutChildren() {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY));
child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
}
}
I called setupLayoutHack() in the constructor but saw no difference after applying those changes either :(
EDIT 4: My final attempt at fixing the SubtitleView layout was experimenting with measuring and laying out in various ways:
private void refreshViewChildrenLayout(View view){
view.measure(
View.MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(), View.MeasureSpec.EXACTLY));
view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
}
However, the height in all cases that I tried was 0, which meant nothing was altered. There is a solution mentioned in the above RN issue that suggests that the shadow node for the subtitle view should be overridden. So one way forward could be to build a new subtitle view that has that included.
However, at this stage it seems to me an easier approach to respond to subtitle cues in React Native and perform all display and styling there.
(There is also a lesser issue of how to make the background on either side of the text transparent, but the layout issue is far more important at this stage).
Disclaimer: I'm not very familiar with React Native and how it influences layout creation if at all.
However looking at your layout file, it indicates that the SubtitleView is the top child of the PlayerView, which is a FrameLayout, thus gets added at the top (left). By specifying android:layout_height="wrap_content" on the SubtitleView it will only take up space that is required by the view. In the Bitmovin sample, it is generated in code and therefore should inherit the attributes from the parent, which is a RelativeLayout with android:layout_weight="1" which results in stretching it's height to the space available.
Long story short, try setting the height of your SubtitleView to match_parent

Android: requestLayout() improperly called

The following error occurs when I attempt to inflate a layout within a ListView:
requestLayout() improperly called by android.widget.TextView{...} during layout: running second layout pass
I am attempting to inflate a layout within a ListView as follows:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) musicActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
...
}else{...}
}
The layout being inflated can look as simple as the following, and will still produce the error
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/txt_size"/>
I have looked into similar questions, and no solutions found seem to work Question 1, Question 2, Question 3.
Does anyone know what causes this type of error? Any troubleshooting advice? For more context, this ListView is displayed within a Fragment within a ViewPager
UPDATE
Here is the full XML Layout (minus a bunch of attributes), that still results in the problem
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Based on this, I would think the XML itself is not a problem, unless it has to do with the fact that I am using a ViewPager and Fragments
This issue seems to be a bug in the android implementation, please see: https://code.google.com/p/android/issues/detail?id=75516
Activating the fast scroll feature of a ListView in your code via ListView.setFastScrollEnabled(true) will trigger this bug and you'll start seeing the
requestLayout() improperly called by android.widget.TextView{...}
during layout: running second layout pass
message in your console.
This bug must have been introduced in one of the KitKat (4.4.x) updates, as I've not seen it with the initial KitKat (4.4.0) release. Apart from the ugly console spamming with the debug message from above, there seem to be no other impacts (maybe performance in some cases, which I haven't tested).
Cheers
PS: it's not the first time that the fast scroll feature is bugged, e.g. https://code.google.com/p/android/issues/detail?id=63545, 63545 was fixed in KitKat 4.4.3 but 75516 poped up thereafter --> seems to be a vexed subject for google ;-)
EDIT May 12 2015:
I updated my Nexus 7 to Android 5.1 some minutes ago (was Running 5.0 before) and stopped seeing this issue in this new version. As the appearance of the FastScroll indicator also changed in 5.1, I assume that google fixed this issue or at least commented out those ugly lines that spammed the console...
75516 & 82461 are still 'unresolved', but I guess that those refer to the same issue, that's now resolved in 5.1.
The problem is that while the method getView() of your adapter is displaying your layout some other code is trying to access this view to display it, resulting in a collision.
Note that some methods, that maybe you don't take care of (like setScale(), setTypeFace()) indeed call requestLayout(), so it would be interesting what you are doing after your inflate statement.
For me this issue was occurring upon a setLayoutParams() call. The solution was posting a runnable on the looper:
// java
imageView.post(new Runnable() {
#Override public void run() {
imageView.setLayoutParams(params);
}
});
// kotlin
post(Runnable { imageView.setLayoutParams(params) })
I fixed this issue by disabling fastScroll on the ListView in the XML.
<ListView
android:id="#+id/mListview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fastScrollEnabled="false"
/>
In my case (Samsung Galaxy S4, API 21) this happened in ListView with EditTexts. I have a listener for field validation. Something like:
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
error.setVisibility(View.INVISIBLE);
error.setText("");
} else {
String s = edit.getText().toString();
if (s.isEmpty()) {
error.setText("Error 1");
} else if (s.length() < 2 || s.length() > 100) {
error.setText("Error 2");
}
error.setVisibility(View.VISIBLE);
}
}
});
After settinging focus in one of these EditTexts an above check is called. After that a TextView will change (the TextView contains an error message and lies over the EditText).
Setting focus to the second or the third EditText led to permanent request of the first EditText and return to current. An applications runs in infinite loop of requests (focus edittext 1, unfocus edittext 1, focus 3, unfocus 3, focus 1, etc).
I tried to set listView.setFastScrollEnabled(false). Also I tried a requestLayout() of some elements like in https://github.com/sephiroth74/HorizontalVariableListView/issues/93 with no chances.
Currently I made that TextView of fixed width and height in XML:
<TextView
android:id="#+id/error"
android:layout_width="match_parent" (or "200dp", but not "wrap_content")
android:layout_height="20dp"
.../>
After some experiments I noticed that a height of 20dp can be replaced with "wrap_content". But if a text is too long that divides into 2 lines, the application again catches in the infinite loop. So, android:singleLine="true" will help. It is deprecated, but amazingly android:maxLines="1" with android:lines="1" don't help as they again request layout.
Eventually we have:
<TextView
android:id="#+id/error"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#f00"
android:textSize="20sp"
tools:text="Error message"/>
That's not a good solution, but at least it breaks the infinite loop.
This might happen if you are using some 3rd party extension of ListView. Replace that with standard ListView and check if it still throws the error.
I had similar problem. Please check Android layout: running second layout pass and my answer.
I had the same issue with Kitkat 4.4.4 on Motorola X with Genymotion. In my case the list item is a simple CheckedTextView and the error occurred in AppCompatCheckedTextView.
As a normal implementation I inflated the item from XML layout file like below:
if (convertView == null) {
convertView = inflater.inflate(R.layout.checkable_list_entry, parent, false);
}
After some trying I found out that this has something to do with XML inflation. I don't know the root cause, but as a solution I decided to inflate the list item by code and set all the properties by code too.
It ended up like this:
CheckedTextView view;
if (convertView == null) {
view = new CheckedTextView(parent.getContext());
view.setMinHeight(getResources().getDimensionPixelSize(R.dimen.default_touch_height));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
view.setTextAppearance(R.style.SectionEntry);
} else {
view.setTextAppearance(parent.getContext(), R.style.SectionEntry);
}
view.setBackgroundResource(R.drawable.form_element);
view.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
} else {
view = (CheckedTextView) convertView;
}
I had a problem with the same warning log :
requestLayout() improperly called by android.support.v7.widget.AppCompatTextView {...} during layout: running second layout pass
I was working with recylcerview and going to update it with new data.
The only solution that worked for me is as below :
Step(1). Remove current data :
public void removeAll() {
items.clear(); //clear list
notifyDataSetChanged();
}
Step(2). When you want to populate the recyclerview with new data, first set a new LayoutManager to recyclerview again:
private void initRecycleView() {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
}
Step(3). Update the recyclerview with new data. for example :
public void refreshData(List newItems) {
this.items = newItems;
notifyItemRangeChanged(0, items.size());
}
Try taking off the textSize from the xml and setting it in Java code. I think that's causing it to be laid out twice.
In my case this warning prevented a button from showing up in API 21 devices. The button visibility was previously set to GONE.
The only workaround I got it was setting to INVISIBLE instead of GONE for API 21. It wasn't a real solution but it was acceptable for me.
I only post this because it can be useful from somebody.
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP) {
theButton.setVisibility(View.INVISIBLE);
}
else {
theButton.setVisibility(View.GONE);
}
Sometimes you maybe already fixed the issue but it still keeps same error, so you need to close visual studio then delete all bin and obj folders from your projects, then uninstall the app from the emulator. then walah!! everything will works fine
I solved the problem like this:
mHolder.txt_fword.setTextSize(22);
mHolder.txt_farth.setTextSize(22);
mHolder.txt_fdef.setTextSize(22);
mHolder.txt_fdef2.setTextSize(22);
mHolder.txt_frem.setTextSize(22);
//if (fdef2.get(pos).equals("")) mHolder.txt_fdef2.setVisibility(View.GONE);
//if (frem.get(pos).equals("")) mHolder.txt_frem.setVisibility(View.GONE);
issue is .setVisibility(View.GONE); , change to .setVisibility(View.VISIBLE);

Strange XML layout bug when targeting Android 4.3

I'm experiencing a really peculiar bug with an XML layout file when building my application while targeting API level 18. It doesn't happen with API level 17. I'm running the application on Android 4.3 devices, and the bug persists on all three devices.
Here's what it looks like:
API 17 (correct):
API 18 (incorrect):
I'm using the StickyGridHeaders library, and the following is my getHeaderView() method:
#Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
RowItem holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.seasons_row_header, parent, false);
holder = new RowItem();
holder.title = (TextView) convertView.findViewById(R.id.seasonTitle);
holder.episodeCount = (TextView) convertView.findViewById(R.id.episodeCount);
convertView.setTag(holder);
} else {
holder = (RowItem) convertView.getTag();
}
if (seasons.get(position).equals("00")) {
holder.title.setText(R.string.stringSpecials);
} else {
holder.title.setText(getString(R.string.showSeason) + " " + seasons.get(position));
}
int size = seasonEpisodeCount.get(Integer.valueOf(seasons.get(position)));
holder.episodeCount.setText(size + " " + getResources().getQuantityString(R.plurals.episodes, size, size));
convertView.setClickable(false);
convertView.setFocusable(false);
return convertView;
}
Here's the layout XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80000000"
android:padding="#dimen/list_padding" >
<TextView
android:id="#+id/seasonTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="#dimen/list_padding"
android:layout_toLeftOf="#+id/episodeCount"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textIsSelectable="false"
android:textStyle="bold" />
<TextView
android:id="#+id/episodeCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/seasonTitle"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/seasonTitle"
android:gravity="bottom"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textIsSelectable="false" />
</RelativeLayout>
Does anyone else have a clue as to what's going on here? I find it really strange that it's working when targeting API level 17 and not working when targeting the latest API level (18).
Update:
This is what it looks like in the visual layout editor with Android 4.3 as the target:
I managed to solve the issue myself.
Like I said, I am using the StickyGridHeaders library and referencing it in my application. It appears that the library was targeting API level 17. I changed it to level 18, compiled and ran the application - great success!
In conclusion: Make sure your application and libraries target the same API level.
I have the same issue, too.
My experiences have shown that crossed alignments are the reason of the problem (TextView #+id/seasonTitle has the property android:layout_toLeftOf="#+id/episodeCount"; TextView #+id/episodeCount has the properties android:layout_alignBottom="#+id/seasonTitle" and layout_alignTop="#+id/seasonTitle". One of the Layouts won't refresh its childs correct.
Changing the Layout-XML that crossed alignments are prevented, you no longer have this problem.
I have the same issue :
a ListView with a white background
each child has a View with a gray background and width and height to match_parent, within a RelativeLayout
on API 17, every is OK
on API 18+, the ListView is white
instead of setting a fixed height, I put a min_height to the View, and the problem is (temporary) solved!
hope it helps

Separator padding , ignoring it

I have a problem with this separator:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<padding android:left="10dip" android:right="10dip"/>
<solid android:color="#color/listSeparator"/>
<size android:height="1px" />
</shape>
I'm trying to make a little margin/padding in the left/right of the listview component (using a relative on it , not a ListView Object) . Then when i try to put it this way...
getListView().setDivider(getResources().getDrawable(R.drawable.song_separator));
... it's directly ignored , putting a full layout separator .
Now i don't know what is the problem , but i know that :
I can't put a margin in all ListView , cause i want the listHeader fill_parent
I have tried to put a false border , but it isn't nice looking when i change it's background color , putting me a separator space.
Any idea?
MODIFIED
My last partial solution is to put an ImageView , aligned next to the parent bottom .
This is partial cause it puts on the bottom but not on the original divider.
If someone can tell me how to put that ImageView on the exact line of the divider, i would give him the +50 too.
Quiroga so my first bet would be to make the code more debugable by spliting the method call up into individual lines.
ListView lview = getListView();
if (lview != null){
Resources res = getResources();
if (res != null) {
Drawable dable = res.getDrawable(R.drawable.song_separator);
if (dable != null){
lview.setDivider(dable)
}
}
} else {
//Log in some way that you have a problem.
}
I know it looks kind of over complicated but that way you can make sure that the Drawable is found and is the correct one and then properly assigned to the ListView.
Another thing you can try is just assigning a different maybe platform specific separator and see if that works properly.
Also try to get your hands on the Android Source Code it is back online if you add that to your Java Project you can debug into the Platform classes and debug deep into the platform code.
So this isn't really a solution for your problem but maybe it can help you find the solution.
Put the header in a separate file and access it as:
public class AuditActivity extends ListActivity {
Budget budget;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audit);
ListView lv = getListView();
LayoutInflater infalter = getLayoutInflater();
ViewGroup header = (ViewGroup) infalter.inflate(R.layout.header, lv, false);
lv.addHeaderView(header);
budget = new Budget(this);
/*
try {
test = budget.getTransactions();
showEvents(test);
} finally {
}
*/
// switchTabSpecial();
}
Follow this link .......it has a detailed info,use RelativeLayout instead of Linear One, I hope this will help you.
Android: Adding static header to the top of a ListActivity

ViewFlipper crashes after orientation change

I'm trying to use inflate while using ViewFlipper to access to the data inside my view. I've done sample project that crashes.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
FlipViewBug.java
package android.FlipViewBug;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
public class FlipViewBug extends Activity {
private static LayoutInflater inflater = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}
When I rotate emulator from horizontal to vertical orientation CTRL+F11 app crashes with stopped unexpectedly.
If I remove line
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
app works fine. Am I trying to do something wrong? In my app I have more complex LinnarView that ViewFlipper is nested and the results are the same.
I was checking this on android 1.5, 2.2 and galaxy tab. There is no problem while rotating form vertical view to horizontal.
As a rule of thumb if you're storing UI objectrefs in static data you're probably doing something wrong. :) Even when things appear to work, you are likely to be leaking memory until Android decides to kill your process. See Romain Guy's article on this for more details.
So basically you answered your own question... don't do that! If you want to delay inflation of flipped-out views until they are flipped-in (i.e. as a performance improvement) I'd suggest you look into ViewStub.

Categories

Resources