Android应用程序上的活动和线程有什么区别?

Sat*_*oro 5 android android-activity

我对Android应用程序上的活动和线程之间的区别感到困惑。那么活动是否像独立线程一样?如果可以,那么多个活动可以同时在多线程应用程序中运行吗?

谢谢

Sho*_*uri 5

我相信您可能已经阅读了什么是活动?之前。如果没有,请这样做。在这里,您可以阅读有关android中进程和线程的更多信息。现在,回答您的问题:

活动是一个独立的线程吗?

每个活动都不是独立线程。正如@ android.h在评论中提到的,所有活动都在同一UI线程上运行。

一个多线程应用程序可以同时运行多个活动吗?

如上所述,所有活动,服务,ContentProviders,BroadcastReceivers等均在UI线程上运行。话虽如此,您可以从一个活动本身内部启动多个线程。因此,您的应用程序可以使用多个线程,但是运行多个活动不会使它成为多线程的。

关于多种活动,您可以阅读“ 任务和后退堆栈”文档。它着重介绍了多项活动的概念:

An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes。这就是多种活动事物的工作方式。

希望这使您的概念更加清晰。


Squ*_*onk 5

那么活动是一个独立的线程吗?

是的,没有。一个Android应用程序只有一个Activity进程和一个线程,但是如果有多个应用程序组件,它们通常都将使用相同的线程(某些Android类使用自己的线程来工作)。

请阅读以下内容...

进程和线程

如果可以的话,多个活动能否同时作为一个多线程应用程序运行?

一个Activity只认为是“跑”时,它是完全可见。例如,当弹出窗口(例如Dialog等)出现时,底层Activity仍部分可见,但将处于“已暂停”状态。如果另一个Activity已启动,并且完全隐藏了前一个(无论是您自己的应用程序还是外部应用程序的一部分),则前一个Activity将进入“停止”状态,甚至可能被销毁。

基本上,Android Activity并不是允许在多线程环境中执行多任务的主力军。An Activity基本上是一个UI框架,用于提供按钮,文本视图,图像等并允许用户交互。

也可以看看...

申请基础

...并在Activity此处查看生命周期图...

活动生命周期


Fer*_*rgo 1

你应该了解UI线程的概念。基本上,有一个主线程(调用活动的回调方法的线程),您可以启动其他线程。您启动的线程无法更新 UI,只能更新 UI 线程。

更多内容请看这里:什么是Android UiThread(UI线程)