即使在调用finish并使引用为null之后,Activity对象仍然存在于内存中

时间:2016-08-14 17:57:39

标签: android performance memory-management retrofit android-memory

我正在从SplashActivity进行改进的入队调用,并通过bundle.Even将数据传递给下一个活动。在显式调用splash活动后,它仍然在MAT中看到保留堆中的48个引用。 该应用程序通常也在后台占用大量内存。我调试并发现我的活动的onDestroy()方法被调用,但为什么对象仍然在内存中持久存在?它是以某种方式与通过引用而不是值传递的数据相关吗?我一直试图在网上搜索,但几乎没有任何运气。如果我在这里看错了方向,请指出我。

这是我的Splash活动的快照 -

public class SplashActivity extends AppCompatActivity {

private String TAG = SplashActivity.class.getSimpleName();

List<Sport> mSportsList;
List<Event> mEventsList;
List<Carousel> mCarouselList;
WatchOnApiService mWatchOnApiService;

@BindString(R.string.package_name)
String mPackageName;

ProgressBar mProgressBar;
AlertDialog.Builder alertDialogBuilder;
AlertDialog alertDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    ButterKnife.bind(this);
    MobileAds.initialize(getApplicationContext(), "ca-app-pub-8433136449848959~7585529227");

    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    mProgressBar.getIndeterminateDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
}

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

private void getSportsAndEventsAndCarousel() {
    mProgressBar.setVisibility(View.VISIBLE);

    WatchOnApiService mWatchOnApiService = RetrofitFactory.getRetrofit().create(WatchOnApiService.class);
    final Call<WatchOnSportsEventsCarousel> mCall = mWatchOnApiService.getAllSportsAndEventsAndCarousel(TimeZone.getDefault().getID());

    mCall.enqueue(new Callback<WatchOnSportsEventsCarousel>() {
        @Override
        public void onResponse(final Response<WatchOnSportsEventsCarousel> response, Retrofit retrofit)
        {
            if (response.isSuccess()) {

                mProgressBar.setVisibility(View.INVISIBLE);

                if (Float.parseFloat(BuildConfig.VERSION_NAME) < Float.parseFloat(response.body().getAndroidLatestVersion())) {
                    if (response.body().getAndroidForceUpdate().equalsIgnoreCase("1")) {

                        showForceUpdateDialog();

                    } else {
                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SplashActivity.this);
                        alertDialogBuilder.setTitle("WatchOn");
                        alertDialogBuilder.setMessage("WatchOn just got better.Would you like to upgrade ?");
                        alertDialogBuilder.setPositiveButton("Upgrade",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int arg1) {
                                        dialog.dismiss();

                                        try {                                                SplashActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + mPackageName)));
                                        } catch (android.content.ActivityNotFoundException anfe) {
                                            SplashActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + mPackageName)));
                                        }
                                    }
                                });
                        alertDialogBuilder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                beginFlow(response);

                            }
                        });

                        AlertDialog alertDialog = alertDialogBuilder.create();
                        alertDialog.show();
                    }
                } else {
                    beginFlow(response);
                }
            }

            //Response not received something went wrong
            else {
                showAlertDialogNoResponse();
            }
        }

        private void beginFlow(final Response<WatchOnSportsEventsCarousel> response) {

            Bundle mBundle = new Bundle();

            mSportsList = CodeUtils.convertSportsListToLocalTime(response.body().getData().getSports());
            Parcelable wrappedSports = Parcels.wrap(new ArrayList<>(mSportsList));
            mBundle.putParcelable(AppConstants.SPORTS_LIST, wrappedSports);
            mEventsList = CodeUtils.convertEventsListToLocalTime(response.body().getData().getEvents());
            Parcelable wrappedEvents = Parcels.wrap(new ArrayList<>(mEventsList));
            mBundle.putParcelable(AppConstants.EVENTS_LIST, wrappedEvents);
            mCarouselList = response.body().getData().getCarousels();
            Parcelable wrappedCarousels = Parcels.wrap(new ArrayList<>(mCarouselList));
            mBundle.putParcelable(AppConstants.CAROUSELS_LIST, wrappedCarousels);

            if ((response.body().getLiveSponsors() == 1)) {
                mBundle.putBoolean(AppConstants.LIVE_SPONSORS_AVAILABLE, true);
            } else {
                mBundle.putBoolean(AppConstants.LIVE_SPONSORS_AVAILABLE, false);
            }

            Intent i = new Intent(SplashActivity.this, SelectSportEventActivity.class);
            i.putExtras(mBundle);
            mSportsList = null;
            mEventsList = null;
            mCarouselList = null;
            startActivity(i);
            SplashActivity.this.finish();
        }

        @Override
        public void onFailure(Throwable t) {
            showAlertDialogInternetFail();
            alertDialogBuilder.setNegativeButton("Retry", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    getSportsAndEventsAndCarousel();
                    dialog.dismiss();
                }
            });
            alertDialog = alertDialogBuilder.create();
            alertDialog.show();

        }
    });
}

@Override
protected void onDestroy() {
    ButterKnife.unbind(this);
    Log.d(TAG, "onDestroy: " + mSportsList + mEventsList + mCarouselList);
    alertDialog = null;
    super.onDestroy();
}

}

0 个答案:

没有答案
相关问题