Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reduce loading time of Activity?

I am doing following in Activity's onCreate,

if(condition satisfied){
    imageView.setImage(passing view to resize);
    imageview.setVisibility(View.VISIBLE);
}
else
    imageview.setVisibility(View.INVISIBLE);

resizing like below,

Bitmap bmSource = BitmapFactory.decodeResource(context.getResources(), res);
Bitmap bmThumbnail;
bmThumbnail = getResizedBitmap(bmSource, getScreenWidth() / x, (getScreenWidth() / y));
Drawable drawable = new BitmapDrawable(context.getResources(), bmThumbnail);
b.setBackground(drawable);

In this code I mentioned one view.but in actual code I am having multiple views This Activity(Consider as A) used as three below,

  1. Outgoing Call Screen
  2. Outgoing Call connected screen
  3. Incoming Call connected screen.

so that I am doing changes in Activity according to that. Using images for speaker,mute,record,etc required for call.

  1. OutGoing Call and Call connected Screen,

a)Doing changes for displaying Outgoing call.

b)Once received call acceptance from other person immediately doing screen changes for call connected screen

c)Once screen changes done, timer will be started for call.

  1. Incoming Call connected,

a) For displaying Incoming call screen used different Activity.

b) Once user accepts call, then moving to Activity A.

c) So in Activity A, initially will do some image set up. Once I got response for acceptance image changes for call connected screen will be done.

So thats the overall process. Now will explain about the problem,

I have mentioned that in both the cases call connected screen changes will be done in Activity A. In first case, as that already in same screen initial image setup will loaded previously and once got response then changes for Call connected screen will be done. Timer will be started. No problem here.

Problem is , When answering Incoming call, it will go to Activity A and Initial image setup. Once got acceptance response will change screen to call connected screen and timer starts. Here it takes time to load initial image setup. So starts timer one sec late leads to different time for Person A and Person B

This problem not occurring in all devices. In Android version 8 I experienced this problem not in above version.

What I meant by Initial Image Setup is setting visibility of imageviews and setting images.

What I have tried, Instead of using Bitmap used Picasso for resizing and setting image. So that I am able to reduce some 0.20 to 0.25 secs. But image is expanded not proper. In above oreo also reduced some 0.x secs. But also not solved the problem.

Also in log, I have seen this

PowerKeeper.Event: notifyActivityLaunchTime: com.package.ex/com.package.ExampleActivity totalTime: 1200

1200 mentioned above is milliseconds taken for launching my Activity?? Sometimes getting 1212, 1400

My app works perfect in above O. But oreo and below takes some time to load that affects the call duration. Its really bad. So what can I do??

So how to reduce time taken for doing UI changes in Activity???

If Anyone find difficulty reading my entire question,then look this short note.

Explained Shortly: Consider you are using a calling app(your device is oreo or lower than oreo).And you receive a Incoming call. You tap on answer. Screen will be changed after successfully connected. What if that screen takes some time to load for you while the person in other end got the screen before you? Call duration for you and the other person will be different. So thats my problem

Note: Facing this problem only in oreo and below verions.

below is my layout for Activity A.(Used as call connected screen and outgoing call screen)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/call_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/connected_call_background"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true">

    <RelativeLayout
        android:id="@+id/top_bar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:paddingLeft="@dimen/activity_vertical_margin"
        android:paddingTop="10dp"
        android:paddingRight="@dimen/activity_vertical_margin"/>

    <ImageView
        android:id="@+id/plain_white_bg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

    <LinearLayout

        android:id="@+id/call_swap_merge_transfer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/center_view"
        android:background="#297B88"
        android:paddingLeft="25dp"
        android:paddingRight="25dp"
        android:visibility="invisible"
        android:weightSum="3">

        <ImageView
            android:id="@+id/swap"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="swapCall" />

        <ImageView
            android:id="@+id/merge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="mergeCall" />

        <ImageView
            android:id="@+id/transfer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="attendedCallTransfer" />
    </LinearLayout>

    <ImageView
        android:id="@+id/record_call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/center_view"
        android:layout_centerHorizontal="true"
        android:onClick="callRecord"
        android:visibility="visible" />

    <View
        android:id="@+id/center_view"
        android:layout_width="match_parent"
        android:layout_height="0.01dp"
        android:layout_centerInParent="true" />

    <ImageView
        android:id="@+id/after_call_connected_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/center_view"
        android:layout_alignParentBottom="true"
        android:background="@drawable/bg_dialpad"
        android:visibility="invisible" />

    <ImageView
        android:id="@+id/conference_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/bg_dialpad"
        android:visibility="invisible" />

    <RelativeLayout
        android:id="@+id/connected_call_root_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:paddingTop="20dp"
        android:paddingStart="40dp"
        android:paddingLeft="40dp"
        android:paddingEnd="40dp"
        android:paddingRight="40dp"
        android:paddingBottom="60dp">

        <TextView
            android:id="@+id/dialling_status"
            style="@style/Base.TextAppearance.AppCompat.Small.Inverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:text="@string/dialling"
            android:textColor="@color/white"
            android:textSize="15.5sp" />

        <TextView
            android:id="@+id/call_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:text=""
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/hangupoutgoingcall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:onClick="hangUpCall" />

        <ImageView
            android:id="@+id/dialerpadbutton_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:clickable="false"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/callspeakerbutton_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:onClick="speakerOn"
            android:visibility="invisible" />

        <View
            android:id="@+id/hangup_view"
            android:layout_width="match_parent"
            android:layout_height=".01dp"
            android:layout_above="@+id/hangupoutgoingcall"
            android:layout_margin="2dp"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/dialerpadbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/hangup_view"
            android:layout_centerHorizontal="true"
            android:layout_margin="10dp"
            android:clickable="false"
            android:onClick="showKeypad"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/callspeakerbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/hangup_view"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            android:onClick="speakerOn"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/callmutebutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/hangup_view"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_margin="10dp"
            android:clickable="false"
            android:onClick="muteOn"
            android:visibility="invisible" />

        <RelativeLayout
            android:id="@+id/connected_call_keypad_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/dialerpadbutton"
            android:layout_marginLeft="-25dp"
            android:layout_marginRight="-25dp">

            <ImageView
                android:id="@+id/bts1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentBottom="true"
                android:layout_toLeftOf="@id/btzoutgoing" />

            <ImageView
                android:id="@+id/btzoutgoing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" />

            <ImageView
                android:id="@+id/bth1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:layout_toRightOf="@id/btzoutgoing" />

            <ImageView
                android:id="@+id/bt71"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/bts1"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@id/bt81" />

            <ImageView
                android:id="@+id/bt81"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/btzoutgoing"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" />

            <ImageView
                android:id="@+id/bt91"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/bth1"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@id/bt81" />

            <ImageView
                android:id="@+id/bt41"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/bt71"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@id/bt51" />

            <ImageView
                android:id="@+id/bt51"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/bt81"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" />

            <ImageView
                android:id="@+id/bt61"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/bt91"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@id/bt51" />

            <ImageView
                android:id="@+id/bt11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/bt41"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@id/bt21" />

            <ImageView
                android:id="@+id/bt21"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/bt51"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" />

            <ImageView
                android:id="@+id/bt31"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/bt61"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_toRightOf="@id/bt21" />

            <View
                android:id="@+id/view2"
                android:layout_width="match_parent"
                android:layout_height="0.005dp"
                android:layout_above="@id/bt21"
                android:background="#4B4B4B"
                android:visibility="invisible" />

            <EditText
                android:id="@+id/editText1aaa"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/buttonoutgoing"
                android:layout_alignBottom="@+id/buttonoutgoing"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_centerInParent="true"
                android:background="#00000000"
                android:ellipsize="start"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:foregroundGravity="center"
                android:gravity="center"
                android:hint=""
                android:paddingRight="10dp"
                android:textColor="@color/black80"
                android:textCursorDrawable="@null" />

            <EditText
                android:id="@+id/contact_nameforoutgoing"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:layout_above="@id/view2"
                android:layout_alignTop="@+id/buttonoutgoing"
                android:layout_alignBottom="@+id/buttonoutgoing"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:background="#00000000"
                android:ellipsize="start"
                android:enabled="false"
                android:focusableInTouchMode="true"
                android:foregroundGravity="center"
                android:gravity="center"
                android:hint=""
                android:textColor="@color/black80"
                android:textCursorDrawable="@null"
                android:textSize="20dp" />

            <Button
                android:id="@+id/buttonoutgoing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@id/view2"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="25dp"
                android:layout_toEndOf="@+id/editText1aaa"
                android:layout_toRightOf="@+id/editText1aaa"
                android:background="#00000000"
                android:onClick="removeNumberFromOutgoingNumber"
                android:textColor="#fff"
                android:visibility="invisible" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/add_hold_transfer_record_holder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/dialerpadbutton"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="25dp"
            android:paddingBottom="10dp">

            <ImageView
                android:id="@+id/hold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:clickable="false"
                android:enabled="false"
                android:onClick="holdOn" />

            <ImageView
                android:id="@+id/call_transfer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:enabled="false"
                android:onClick="unAttendedCallTransfer" />

            <ImageView
                android:id="@+id/addcall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentBottom="true"
                android:enabled="false"
                android:gravity="bottom"
                android:onClick="addCall"
                android:textColor="@color/white" />

            <ImageView
                android:id="@+id/record_call_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:onClick="callRecord"
                android:visibility="invisible" />

        </RelativeLayout>
    </RelativeLayout>

    <ListView
        android:id="@+id/listview_status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_bar"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:dividerHeight="-1dp"
        android:gravity="center"
        android:visibility="invisible" />
</RelativeLayout>

Please anybody help me... Share some ideas to resolve this issue...Thanks in advance

like image 800
Kousalya Avatar asked Nov 06 '22 06:11

Kousalya


People also ask

What is loading time of app?

An important measure of user experience is the amount of time it takes for user mode applications to initialize fully before their user interface becomes accessible (clickable) by the user. Applications that are slow to load may indicate system issues, resource bottlenecks, and user frustration.

Why are my apps so slow to open?

1. Scan Your System for Virus or Malware. One of the main reasons for Windows 10 apps slow to open issue is that your PC might be infected with a virus that slows your Windows 10, 8, or 7 PC response time. You can use Windows Defender or a third-party antivirus program to scan your system and remove virus or malware.


1 Answers

I have a few suggestions:

  1. Use ConstraintLayout, your xml file contains many views and you are using RelativeLayout or LinearLayout and these are used in a nested way, so this layout nesting may impact performance.
  2. Another thing is while using ConstraintLayout use should use layout_constraintDimensionRatio for you ImageView to set the imageView height or width according to the aspect ratio of the image which you are going to load. So one this is you need to fix either height or width, and the other thing will be calculated by the aspect ratio.

I am just sharing part of ImageView code, It would be like this:

<androidx.constraintlayout.widget.ConstraintLayout
        
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <ImageView
        
        android:layout_width="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintDimensionRatio="H,16:9"  
        android:layout_height="0dp"> </androidx.constraintlayout.widget.ConstraintLayout>

Note the : app:layout_constraintDimensionRatio="H,16:9"

Also, use glide to load images.

  1. And instead of starting a new activity, you should use fragment, we have seen fragment load faster than starting a new activity.

Try the above-mentioned things, and let me know if the issue persists.

like image 192
Suraj Vaishnav Avatar answered Nov 15 '22 05:11

Suraj Vaishnav