按下后退按钮时释放应用程序的所有内存

时间:2016-11-05 01:41:34

标签: android android-fragments android-activity memory-management

我有问题,即使我按回按钮也没有释放内存。如果我下次打开应用程序然后它将添加到现有内存。我有以下代码

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {



    ImageView my_flash_screen;
    DB db;

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

        my_flash_screen= (ImageView) findViewById(R.id.my_flash_screen);

        Picasso.with(this)
                .load(R.drawable.mynew)
                .into(my_flash_screen);
        //    my_flash_screen.setBackgroundResource(R.drawable.mynew);


        if(FontUtil.storeSharedPreference(getApplicationContext()).getString("font", "").isEmpty()) {

            MainTask task = new MainTask();
            task.execute();
            Toast.makeText(this,"first",Toast.LENGTH_LONG).show();
            Log.d("Test","first");
        }
            Log.d("Test","second");
        Toast.makeText(this,"second",Toast.LENGTH_LONG).show();
        AnimationSet animation = new AnimationSet(true);
        animation.addAnimation(new AlphaAnimation(0.0F, 1.0F));
        animation.addAnimation(new ScaleAnimation(0.0f, 1, 0.0f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)); // Change args as desired
        animation.setDuration(1500);

       my_flash_screen.startAnimation(animation);

        new CountDownTimer(4000, 1000) {

            public void onTick(long millisUntilFinished) {

            }

            public void onFinish() {


                Intent i = new Intent(MainActivity.this, NavigationActivity.class);
                my_flash_screen=null;
                startActivity(i);
                finish();

            }
        }.start();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private class MainTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            if(FontUtil.storeSharedPreference(getApplicationContext()).getString("font", "").isEmpty())
            {
                FontUtil.savePrefs("font", "Roboto.TTF",getApplicationContext());
                FontUtil.savesize("fb",22, getApplicationContext());

                Log.d("Test","insidefont");
            }

            try {
                if(db==null){
                    db=new DB(getApplicationContext());
                    db.createdatabase();
                    Log.d("Test","intialise first");
                }
                Log.d("Test","intialise second");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

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


    @Override
    protected void onDestroy() {
        super.onDestroy();

        my_flash_screen=null;
        finish();
       // Runtime.getRuntime().gc();
        System.gc();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

        my_flash_screen=null;
        finish();
        System.gc();
        //Runtime.getRuntime().gc();
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:gravity="center"
    android:layout_weight="1"
    android:orientation="vertical">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/my_flash_screen"
        android:contentDescription="loading image" />
</LinearLayout>

我的问题是我的应用程序会在打开应用程序3到4次后触发并关闭

即使我尝试了很多方法但没有使用

我正在使用80 kb图像,这是jpg压缩图像。我很确定这是因为当我按下后退按钮时可绘制的图像不释放内存。闪存屏幕后导航抽屉将启动并在那导航抽屉两个片段包含单个图像。即使我尝试使用picassa库。

1.当按下后退按钮或销毁时,是否有任何选项可以释放可绘制的图像内存? 2.无论如何在按下后退按钮时释放应用程序的整个内存?

谢谢

enter image description here

0 个答案:

没有答案