【问题标题】:Activity does not scroll smoothly when resume multiple times多次恢复时活动不平滑滚动
【发布时间】:2021-09-11 19:09:04
【问题描述】:

我的android应用程序有一个严重的问题,就是当activity多次(大约10次)恢复时,滚动时activity会变得滞后。如果我开始一个新的活动,新的活动可以顺利进行,但如果我让它恢复大约 10 次,它也会像第一个一样滞后。我尝试过使用简单的 UI,只有一个 ScrollView 和几个 TextViews。下面是我的类实现:

public class TestLagActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onBackPressed() { //make activity resume when press back button 
        for(int i=0;i<11;i++)
            onResume();
    }
}

有人遇到过这样的情况吗?您能否建议我一些可能导致此类问题的原因?如果您需要更多代码,请告诉我,我会提供给您。

提前谢谢你!

更新: 当我按下返回按钮使活动恢复时(参见上面的 onBackPressed() 方法),活动滞后,logcat 显示:

2020-03-13 21:53:42.731 22255-22314/com.mypackagename.dev V/FA: Inactivity, disconnecting from the service
        2020-03-13 21:53:44.279 22255-22255/com.mypackagename.dev D/KeyEvent: obtain, mHwFlags=1768911460
        2020-03-13 21:53:44.286 22255-22255/com.mypackagename.dev D/KeyEvent: obtain, mHwFlags=1768911460
        2020-03-13 21:53:44.290 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148835
        2020-03-13 21:53:44.307 22255-22314/com.mypackagename.dev V/FA: Connecting to remote service
        2020-03-13 21:53:44.311 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148846
        2020-03-13 21:53:44.328 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.328 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148855
        2020-03-13 21:53:44.345 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.346 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148867
        2020-03-13 21:53:44.362 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.363 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148876
        2020-03-13 21:53:44.379 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.380 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148887
        2020-03-13 21:53:44.397 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.397 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148896
        2020-03-13 21:53:44.413 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.414 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148907
        2020-03-13 21:53:44.427 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.428 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148921
        2020-03-13 21:53:44.432 22255-22290/com.mypackagename.dev I/zygote64: Starting profile saver IsSaveProfileNow end.
        2020-03-13 21:53:44.440 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.441 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148930
        2020-03-13 21:53:44.453 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.454 22255-22314/com.mypackagename.dev V/FA: Activity resumed, time: 16148942
        2020-03-13 21:53:44.466 22255-22314/com.mypackagename.dev V/FA: Connection attempt already in progress
        2020-03-13 21:53:44.467 22255-22314/com.mypackagename.dev D/FA: Connected to remote service
        2020-03-13 21:53:44.470 22255-22314/com.mypackagename.dev V/FA: Processing queued up service tasks: 11
        2020-03-13 21:53:44.549 22255-22262/com.mypackagename.dev I/zygote64: Do partial code cache collection, code=251KB, data=151KB
        2020-03-13 21:53:44.549 22255-22262/com.mypackagename.dev I/zygote64: After code cache collection, code=251KB, data=151KB
        2020-03-13 21:53:44.549 22255-22262/com.mypackagename.dev I/zygote64: Increasing code cache capacity to 1024KB

更新 2: 活动的 xml 文件:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="@string/verify" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="@string/verify" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="@string/verify" />

    <!--Other TextViews-->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="@string/verify" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="@string/verify" />
</LinearLayout>

更新 3: 项目中使用的依赖项。

implementation 'io.supercharge:shimmerlayout:2.1.0'
implementation 'com.facebook.shimmer:shimmer:0.5.0'
implementation "com.rizlee.view:rangeseekbar:1.0.0"
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
implementation 'com.mikhaellopez:circularimageview:3.0.2'
implementation 'com.makeramen:roundedimageview:2.3.0'
implementation 'com.github.florent37:singledateandtimepicker:1.2.2'
implementation 'me.relex:circleindicator:1.2.2@aar'
implementation 'com.wang.avi:library:2.1.3'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.facebook.android:facebook-android-sdk:5.11.0'
implementation 'com.facebook.android:account-kit-sdk:4.39.0'
implementation 'com.facebook.android:facebook-share:5.11.0'
implementation 'com.google.android.gms:play-services-base:17.1.0'
implementation 'com.google.android.gms:play-services-analytics:17.0.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-vision:19.0.0'
implementation 'com.google.android.libraries.places:places:2.2.0'
implementation 'com.google.android.gms:play-services-places:17.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.maps.android:android-maps-utils:0.5'
implementation 'com.google.android.gms:play-services-plus:17.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.0'
implementation 'com.google.firebase:firebase-analytics:17.2.3'
implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.firebase:firebase-firestore:21.4.1'
implementation 'com.google.firebase:firebase-messaging:20.1.2'
implementation 'com.appsflyer:af-android-sdk:4.9.0'
implementation 'com.android.installreferrer:installreferrer:1.1.2'
implementation(name:'HERE-sdk-lite', ext:'aar')
implementation 'io.branch.sdk.android:library:4.4.0'

【问题讨论】:

  • 请将您的活动源代码添加到您的评论中。
  • @感谢您的关注,正如我上面提到的,活动是从 AppCompatActivity 扩展而来的,我只是重写了 onCreate() 方法。在这个方法中,我只是setContentView(R.layout.activity_ui),我没有在activity内部声明任何实例字段
  • 你为什么要调用 onResume 10(实际上是 11)次?
  • @EpicPandaForce 因为在我的应用程序中,活动在恢复大约 5 到 10 次时变得滞后。因此,我编写了这个函数来执行这种情况以便更快地测试。
  • 但是您的onResume 似乎是空的,所以这很奇怪?

标签: java android


【解决方案1】:

一周后,我发现implementation 'com.facebook.android:facebook-android-sdk:5.11.0' 使我的应用程序中的活动在恢复后变得滞后。我通过这个answer解决了它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多