Android的处理程序改变了WeakReference [英] Android Handler changing WeakReference

查看:171
本文介绍了Android的处理程序改变了WeakReference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的静态处理程序有一个的WeakReference 我的活动(这是prevent有据可查内存泄漏问题)。

My static handler has a WeakReference to my Activity (this is to prevent the well documented memory leak issue).

我发表拖延已久的消息,我希望这个信息传递给我的活动(这应该是在前台)。

I post a long delayed message and I want this message delivered to my activity (which should be in the foreground).

我担心的是对方向的变化,我的活动被破坏,处理程序引用了旧的活动应该已被销毁。

My concern is that on orientation change, my activity is destroyed and the handler has a reference to the old activity which should have been destroyed.

在为了解决这个问题在我的的onCreate 为我做到这一点的活动。

In order to get around this in my onCreate for the activity I do this.

    if(mHandler == null)
        mHandler = new LoginHandler(this);
    else {
        mHandler.setTarget(this);
    }

和我的处理程序被声明为静态的全局变量:

And my handler is declared as a static global variable:

private static LoginHandler     mHandler            = null;

和实现类也是静态的,如下:

and the implementing class is also static as below:

private static class LoginHandler extends Handler {

    private WeakReference<LoginActivity>    mTarget;

    LoginHandler(LoginActivity target) {
        mTarget = new WeakReference<LoginActivity>(target);
    }

    public void setTarget(LoginActivity target) {
        mTarget = new WeakReference<LoginActivity>(target);
    }

    @Override
    public void handleMessage(Message msg) {
        // process incoming messages here
        LoginActivity activity = mTarget.get();
        switch (msg.what) {
            case Constants.SUCCESS:
                activity.doSomething();
                break;

            default:
                activity.setStatusMessage("failed " + msg.obj, STATUS_TYPE_DONE);
        }
    }
}

如果有什么不对的变化对的onCreate 或有别的毛病此办法的WeakReference我想知道的是?

What I want to know is if there is something wrong with changing the WeakReference on onCreate or is there anything else wrong with this approach?

谢谢

推荐答案

所以我写了下面的测试,以找出是否我有个好主意与否,似乎是m的做法是正确的。在的onCreate 我们修改的WeakReference 和投递的邮件将始终被传递到在前台活动。如果更改此code总是创建的onCreate 一个新的处理程序,你会发现更新消息没有得到传递。

So I wrote the following test to figure out whether I had the right idea or not and it seems that m approach is correct. In onCreate we change the WeakReference and the posted message will always get delivered to the activity that is in the foreground. If you change this code to always create a new Handler in onCreate you'll notice the update messages do not get delivered.

public class MainActivity extends Activity {

    private static int COUNT = 0;

    static LoginHandler mHandler;

    private static class LoginHandler extends Handler {

        private WeakReference<MainActivity> mTarget;

        LoginHandler(MainActivity target) {
            mTarget = new WeakReference<MainActivity>(target);
        }

        public void setTarget(MainActivity target) {
            mTarget.clear();
            mTarget = new WeakReference<MainActivity>(target);
        }

        @Override
        public void handleMessage(Message msg) {
            // int duration = Toast.LENGTH_LONG;
            // process incoming messages here
            MainActivity activity = mTarget.get();
            activity.update(msg.arg1);
        }
    }

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

        if(mHandler == null)
            mHandler = new LoginHandler(this);
        else
            mHandler.setTarget(this);

        ((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.arg1 = COUNT++;
                mHandler.sendMessageDelayed(msg, 3000);

            }
        });

    }

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

    private void update(int count) {
        ((TextView) findViewById(R.id.hello_world)).setText("Hello World @ "+ count);
    }

}

这篇关于Android的处理程序改变了WeakReference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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