Slow transition of Activities: multiple "initializing inflate state" in LogCat
Asked Answered
S

1

6

In order to provide a custom typeface in my ListActivity, I wrote a class CustomAdapter extending BaseAdapter according to this example here.

However, as described there, I wrote the getView() method like following:

public View getView(int position, View convertView, ViewGroup parent){
    String gameName = gameNames[position]; // gameName ist the String[] of the Custom Adapter

    TextView tv = new TextView(context);
    tv.setText(gameName);
    tv.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/gulim.ttf"));

    return tv;
}

This works as intended. The only disturbing thing is that it takes about three or four seconds until the list shows up (and this is a very long time in this context). However, in the ListActivity I set the onItemClickListeners like this:

private void setOnItemClickListener(){
    getListView().setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id){
            onClickEntryButton(((TextView) view).getText().toString());
        }
    });
}

private void onClickEntryButton(String gameName){
    Intent intent = new Intent(this, GameActivity.class);
    intent.putExtra("gameName", gameName);
    startActivity(intent);
    finish();
}

Now when clicking on a ListItem, it takes even more time until the GameActivity opens. This Activity is just a couple of TextViews filled with information taken from a SQLite database. Also here, I set a custom typeface to every TextView. It happens even that the screen gets black for 2-3 seconds (appearing the app crashed), then the new Activity shows up. This doesn't happen accessing that Activity from other places in the application.

In both cases - accessing the ListActivity and accessing the GameActivity from the ListActivity - a couple of

"szipinf - Initializing inflate state"

messages appear in the LogCat. What does they mean in this context? Would it be a better usage to set the onItemClickListeners into the getView() method of my CustomAdapter? Something seems to really inhibit the transitions, but I can't figure out what, since there is nothing big to be calculated or processed (in fact, in the SQLite database are exactly two entries with each 5 fields)?

EDIT If required or desired, of course I can provide more code.

Striated answered 27/3, 2013 at 20:46 Comment(0)
M
4

I had exactly same issue and your question gave me answer!

I still do not know exact root cause but this the isuue is because of reading custom font from assets multiple times in my case. If I have 10 widgets in my screen and each of them is using custom font Android loads it from assets everytime. which was not just causing my activity transions to become slow it was also resulting in crash after playing with it multiple tmes.

So I created a cache for my typeface to avoid fetching typeface everytime from assets.

I added this code inside my utility class:

private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

public static final String ASSET_PATH="assetPath";

public static Typeface getFont(Context c, String assetPath) {
    synchronized (cache) {
        if (!cache.containsKey(assetPath)) {
            try {
                Typeface t =(Typeface.createFromAsset(c.getAssets(),
                        "fonts/arial.ttf"));
                cache.put(assetPath, t);
            } catch (Exception e) {
                return null;
            }
        }
        return cache.get(assetPath);
    }
}

I created my custom class to to setTypeface

public class MyButton extends Button {

public MyButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public MyButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyButton(Context context) {
    super(context);
}

@Override
public void setTypeface(Typeface tf) {

    super.setTypeface(Util.getFont(getContext(), Util.ASSET_PATH));
}

}

variable assetPath can be used to provide diffrent fonts at runtime

EDIT: Here is custom typefaceManager I have created as a library to make it more generic.

Mediatize answered 12/3, 2014 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.