GAMA
GAMA

Reputation: 5996

SetContentView() taking time even though layout is very simple and light

My setContentView() is taking around 3 seconds to load even though it has very simple layout withouht any custom views.

xml layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/homeScreenLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

<RelativeLayout
    android:id="@+id/mainview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_home"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/homeButtonsLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" >

            <Button
                android:id="@+id/btnHomeSearch"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_centerInParent="true"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="30dp"
                android:background="@drawable/some_drawable" />

            <TextView
                android:id="@+id/tvHomeSearch"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/btnHomeSearch"
                android:gravity="center_horizontal"
                android:paddingTop="5dp"
                android:text="Some text"
                android:textColor="#fff"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" >

            <Button
                android:id="@+id/btnHomeClip"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_centerInParent="true"
                android:layout_marginTop="30dp"
                android:background="@drawable/some_drawable" />

            <TextView
                android:id="@+id/tvHomeClip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/btnHomeClip"
                android:gravity="center_horizontal"
                android:paddingTop="5dp"
                android:text="Some text"
                android:textColor="#fff"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="20dp" >

            <Button
                android:id="@+id/btnSeeAll"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_centerInParent="true"
                android:layout_marginTop="30dp"
                android:background="@drawable/some_drawable" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/btnSeeAll"
                android:gravity="center_horizontal"
                android:paddingTop="5dp"
                android:text="Some text"
                android:textColor="#fff"
                android:textSize="20sp" />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>


Below is the piece of onCreate():

Log.i("PN","Main onCreate() start");
setContentView(R.layout.main_flipper);
Log.i("PN","Main onCreate() aftr setContentView");

this ViewFlipper has only 2 layouts. 2nd layout is also lightweight.


Corresponding Log:

11-12 09:23:01.622: I/PN(12081): Main onCreate() start
11-12 09:23:04.324: I/PN(12081): Main onCreate() aftr setContentView

Note that setContentView() is taking approx 3 seconds which is quite high.

Any idea why this is happening?

Any help appreciated.

Edit

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        Log.i("PN","Main onCreate() start");
        // Set the design screen for the code
        // Activate full screen for this flow
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main_flipper);
        Log.i("PN","Main onCreate() aftr setContentView");

        Bundle b = getIntent().getExtras();
        Log.i("PN","Main onCreate() aftr getExtras");
        int viewNo = 0;
        if (b != null) {
            viewNo = b.getInt("View");
        }

        initVariables();

        setOnClickListeners();
        initSearchView();
        setTextWatcher();
        vf.setDisplayedChild(viewNo);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

updated Log:

11-12 09:42:12.532: I/PN(14342): Main onCreate() start = 1384249332537
11-12 09:42:15.122: I/PN(14342): Main onCreate() aftr setContentView = 1384249335125
11-12 09:42:15.142: I/PN(14342): Main onCreate() aftr getExtras = 1384249335142
11-12 09:42:15.142: I/PN(14342): Main onCreate() b4 init = 1384249335142
11-12 09:42:15.392: I/PN(14342): Main onCreate() End = 1384249335396

As we can see, main delay is happening in setting up setContentView()

Edit (2)

updated Log:

11-12 09:48:11.342: I/PN(15081): Main onCreate() start = 1384249691345
11-12 09:48:11.372: I/PN(15081): Main onCreate() aftr FLAG_FULLSCREEN = 1384249691372
11-12 09:48:13.961: I/PN(15081): Main onCreate() aftr setContentView = 1384249693970

Upvotes: 1

Views: 1864

Answers (1)

Calvin
Calvin

Reputation: 3312

You needs to unload item by item to see which is the cause. My suggestion:

  1. Remove background drawables and test.
  2. Remove each of the 3rd layer relative layout and test.

p/s: I think You can actually reduce one layer of relative layout. The second layer of relative layout could be merged with the first one.

Upvotes: 1

Related Questions