[英]Android application optimal design with several activities
I'm developing a quite big app with several activities, see link , and I have 2 questions about the base design. 我正在开发一个具有多个活动的大型应用程序, 请参阅link ,我对基本设计有两个问题。
What is best practise concerning opening activities, so that that i don't waste memory by having multiple instances of the same class open at same time and such? 关于开放活动的最佳实践是什么,这样我就不会因为同时打开同一类的多个实例而浪费内存?
The app must receive USB-data constantly through a UART-interface, and should somehow forward this data to the activity in focus. 该应用程序必须通过UART接口不断接收USB数据,并应以某种方式将此数据转发给重点活动。 For now the start activity receive the data through a handler. 现在,开始活动通过处理程序接收数据。 this would work if only one activity needed the USB-data. 如果只有一项活动需要USB数据,则可以使用此功能。 How should I do this? 我应该怎么做?
Start.java 启动.java
final Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
if(actualNumBytes[0] != 0x00)
{
info.append(String.copyValueOf(readBuffer, 0, actualNumBytes[0]));
}
}
};
handler_thread.java handler_thread.java
/*usb input data handler*/
private class handler_thread extends Thread
{
Handler mHandler;
handler_thread(Handler h ){
mHandler = h;
}
public void run()
{
while(true)
{
Message msg = mHandler.obtainMessage();
try{
Thread.sleep(50);
}
catch(InterruptedException e){}
status = uartInterface.ReadData((byte)64, readBuffer, actualNumBytes);
mHandler.sendMessage(msg);
}
}
}
If you wish to minimize the number of activities, consider using fragments instead. 如果希望减少活动数量,请考虑改用片段。
If you have a global variable/event/thread that need to be handled by the current activity, put it in a service, and let the activity communicate with it (connect on start/resume, disconnect on pause/stop). 如果您有一个需要由当前活动处理的全局变量/事件/线程,请将其放在服务中,并让该活动与其通信(在启动/恢复时连接,在暂停/停止时断开连接)。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.