简体   繁体   English

恢复行为后的Android处理程序

[英]Android Handler after resume behavior

I have an Android app which receives messages via Bluetooth (but that part is not relevant here). 我有一个Android应用程序,可通过蓝牙接收消息(但此处不相关)。 The messges are received by a handler. 消息由处理程序接收。 When starting the app after it has been killed by the Android task manager it works as it should. 在被Android任务管理器杀死后启动应用程序时,它可以正常工作。 If I don't kill it and just bring it back to front it doesn't behave like it should. 如果我不杀了它,而只是把它放回了前面,那它就不会表现出应有的状态。 The code is quite simple: 代码很简单:

public class MainActivity extends Activity 
{

private BtBase mBtBase;
private TextView mTvBluetooth;
private View mView;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    mBtBase = BtBase.getInstance(this, mHandler);
    setContentView(R.layout.bluetooth);
}

@Override
protected void onResume()
{
    super.onResume();
    mTvBluetooth = (TextView) findViewById(R.id.tvBluetooth);
    if(mBtBase.getErrorCode() == BtLibraryConstants.BT_CONNECTED)
    {
        mTvBluetooth.setText("Text 1");
    }
    else
    {
        mTvBluetooth.setText("Text 2");
    }

}


private final Handler mHandler = new Handler() 
{
    @Override
    public void handleMessage(Message msg) 
    {
        switch(msg.what)
        {
        case 0:
            if(msg.arg2 == 0)
            {
                Toast.makeText(getApplicationContext(), "Text 1", Toast.LENGTH_LONG).show();
                mTvBluetooth.setText("Text 1.");
            }
            else if(msg.arg2 == 1)
            {
                Toast.makeText(getApplicationContext(), "Text 2", Toast.LENGTH_LONG).show();
                mTvBluetooth.setText("Text 2");
            }
            break;
        case 1:
            Toast.makeText(getApplicationContext(), (String) msg.obj, Toast.LENGTH_LONG).show();
            mTvBluetooth.setText("Text 3: " + (String) msg.obj);

            break;
        default:

        }
    }
};  
}

What DOES work after starting app from a resume situation: 从简历状态启动应用程序后,什么工作:

  • the toast messages in the handler are displayed correctly 处理程序中的吐司消息正确显示
  • the textview in onResume has the right value depending on the state onResume中的textview根据状态具有正确的值

What does NOT work after starting app from a resume situation: 从简历状态启动应用后,什么不起作用:

  • The text view update in the handler is not working. 处理程序中的文本视图更新不起作用。

Keep in mind the toast works correctly, so the code is executed. 请记住,吐司工作正常,因此代码被执行。 Keep in mind it works fine after killing the app from the task manager, so it is not completely wrong. 请记住,从任务管理器中删除该应用程序后,它可以正常工作,因此,这并不是完全错误的。 I assume it has something with the lifecycle management to do. 我认为这与生命周期管理有关系。

I tried several things 我尝试了几件事

  • passed the main looper into the handler, though since I create the handler from the main thread it shouldn't be necessary 将主循环程序传递到处理程序中,尽管由于我是从主线程创建处理程序的,所以不需要
  • got the main view and invalidated it in the handler to force a redraw 得到了主视图并使其在处理程序中无效以强制重绘

Neither worked, but of course I might have done something wrong in these tries. 两者都不起作用,但是当然在这些尝试中我可能做错了什么。

The debugger shows that my textView mTvBluetooth is not null and it looks like it is the one I am looking for. 调试器显示我的textView mTvBluetooth不为null,并且看起来像是我要寻找的那个。 But maybe a new mTvBluetooth is created somehow and I am using the old mTvBluetooth. 但是也许以某种方式创建了新的mTvBluetooth,而我正在使用旧的mTvBluetooth。

Last but not least logCat doesn't show any errors. 最后但并非最不重要的一点是logCat不会显示任何错误。

Try to move this code 尝试移动此代码

mTvBluetooth = (TextView) findViewById(R.id.tvBluetooth);

from onResume() to onCreate() method. onResume()onCreate()方法。 Maybe it helps. 也许有帮助。

Also, TextView is not recreated when activity is resumed in common way. 此外,以常规方式恢复活动时不会重新创建TextView。 But sometimes it can be destroyed buy the system to obtain more memory. 但是有时可以销毁它来购买系统以获得更多内存。 In such may, when you restore activity, method onCreate must be called before onResume . 在这种情况下,当您还原活动时,必须在onResume之前调用方法onCreate

If you don't want to move code, try to make mTvBluetooth volatile . 如果您不想移动代码,请尝试使mTvBluetooth 易失 Try to see mTvBlutooth value in mHandler and onResume() while debugging. 尝试在调试时尝试在mHandler和onResume()中查看mTvBlutooth值。 Use "Inspect" tool in eclipse (Ctrl+Shift+I), you will see an variable id, eg 830032407656. Check ids at these two moments. 在Eclipse中使用“检查”工具(Ctrl + Shift + I),您将看到一个变量ID,例如830032407656。在这两个时刻检查ID。

Please leave a comment is an answer helpful. 请发表评论是有帮助的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM