闪屏内存泄漏怎么解决? [英] Splash screen leaking memory how to fix it?

查看:68
本文介绍了闪屏内存泄漏怎么解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的闪屏内存泄漏谁能告诉我如何解决这个问题?

My Splash screen leaking memory can anyone tell me how to fix that issue ?

public class SplashActivity extends AppCompatActivity {

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

    new InnerClass(SplashActivity.this).loadNext();
}

private static class InnerClass {
    private final WeakReference<Activity> weakReference;

    private InnerClass(Activity activity) {
        this.weakReference = new WeakReference<>(activity);
    }

    private void loadNext() {
        Activity context = weakReference.get();
        if (context != null) {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    context.startActivity(new Intent(context, NavigationActivity.class));
                    context.finish();
                }
            }, 2000);
        }
    }
}

}

这是泄漏金丝雀库报告显示您的活动泄漏内存

Here is the leak canary library report showing your activity leaking memory

推荐答案

weakReference.get() 方法移到 run() 方法中.请尝试以下:

Move the weakReference.get() method inside the run() method. Please try below:

private void loadNext() {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Activity context = weakReference.get();
                    if (context != null) {
                    context.startActivity(new Intent(context, NavigationActivity.class));
                    context.finish();
                   }
                }
            }, 2000);
         }
        

这篇关于闪屏内存泄漏怎么解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆