Android on销毁并完成 [英] Android onDestroy and finish

查看:162
本文介绍了Android on销毁并完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些有关onDestroy和finish之间区别的答案.

I found some answer about the difference between onDestroy and finish.

完成会从堆栈中删除活动,但不会释放活动 内存,并且不会每次都调用onDestroy. onDestroy将终止活动并释放内存.

Finish will remove the activity from the stack, but will not free the memory and will not call onDestroy in every time. onDestroy will kill the activity and free the memory.

在我的情况下: 有2个活动. A是主要活动,B是带有一些EditText的活动,用于通过Volley将数据发送到服务器.

In my situation: There are 2 activities. A is main activity and B is a activity with some EditText for sending data to server by using Volley.

A-> B-> A

A -> B -> A

当我成功发送数据时,它将运行finish()杀死B. B被摧毁.但是我再次调用B,所有数据仍然存在(EditText的内容).这意味着内存尚未清除.

When i sent the data successfully.It will run the finish() to kill B. B is destroyed. But i call B again, all the data still there (content of EditText). Which means the memory haven't clear.

有人在那些问题上面临吗? 有我的代码:

Did anyone face on those issue? There are my code:

public void update(final Context context, final Map<String,String> data,final Activity activity){//pass B
        RequestQueue queue = Volley.newRequestQueue(context);
        String url = "http://xxxxxxxx";
        StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                activity.finish();



            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //mPostCommentResponse.requestEndedWithError(error);
            }
        }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = data;
                //data.
                //params.put("user",data.get(''));


                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                params.put("Content-Type","application/x-www-form-urlencoded");
                return params;
            }
        };
        queue.add(sr);
    }

已更新

活动B通过使用FragmentStatePagerAdapter具有viewpager和3个片段. 当我调用B的finish()时,这些片段下的数据将无法清除.当我再次开始B活动时,我可以看到旧数据.

Activity B have viewpager and 3 fragment by using FragmentStatePagerAdapter. When i call finish() of B. Those data under the fragments won't clear. I can saw the old data when i start B activity again.

推荐答案

调用以下函数时:

someActivity.finish();  // This will free the memory, see notes below

应注意,其所有资源均已排队以进行垃圾收集,并且该活动使用的所有内存将在下一个GC周期期间释放.

It should be noted that all its resources are queued for garbage collection, and all memory that was used by this activity will be freed during next GC cycle.

如果您真的想尽快撤消内存,请覆盖您活动的onDestroy方法:

If you really want to revoke the memory as soon as possible, override your activities' onDestroy method:

@Override
public void onDestroy() {
    super.onDestroy();
    Runtime.getRuntime().gc();      // notify gc to be called asap
}

有关 Runtime.getRuntime().gc(); 的注意事项,这要求VM尽最大努力回收所有丢弃的对象-从

Note about Runtime.getRuntime().gc(); this asks the VM to make its best effort to recycle all discarded objects--quoted from http://www.tutorialspoint.com/java/lang/runtime_gc.htm.

这篇关于Android on销毁并完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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