全局context,关闭程序,在哪都退出

本文介绍了一个用于全局管理多个Activity的应用框架实现方式。通过创建MyApplication类来管理Activity的生命周期,并提供了一个静态方法获取全局Context。此外,还实现了退出应用程序时遍历所有Activity并关闭的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个就不多说了,无意中看到的一个小知识点,做下记录:

直接上代码

public class MyApplication extends Application {
	private static Context context;


	private List<Activity> activityList = new LinkedList<Activity>();


	// 添加Activity到容器中
	public void addActivity(Activity activity) {
		activityList.add(activity);
	}


	// 遍历所有Activity并finish
	public void exit() {
		XmppConnectionManager.getInstance().disconnect();
		for (Activity activity : activityList) {
			activity.finish();
		}
	}


	@Override
	public void onCreate() {


		context = getApplicationContext();
	}


	// 以后使用上下文context时只需调用MyApplication.getContext()即可。。
	public static Context getContext() {
		return context;
	}
}
大家都知道,MyApplication里面是初始化一些全局的东西

如果你想要关闭渲染但不退出程序,可以修改示例代码来实现。以下是修改后的示例代码: ```cpp #include <Windows.h> #include <d3d11.h> LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); HWND g_hWnd; ID3D11Device* g_pDevice = nullptr; ID3D11DeviceContext* g_pDeviceContext = nullptr; IDXGISwapChain* g_pSwapChain = nullptr; bool g_isRendering = true; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 创建窗口 WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WindowProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("DX11App"), NULL }; RegisterClassEx(&wc); g_hWnd = CreateWindow(wc.lpszClassName, _T("DX11 Application"), WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, NULL, NULL, wc.hInstance, NULL); // 初始化 DirectX 设备和交换链 DXGI_SWAP_CHAIN_DESC swapChainDesc = { 0 }; swapChainDesc.BufferCount = 1; swapChainDesc.BufferDesc.Width = 800; swapChainDesc.BufferDesc.Height = 600; swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.OutputWindow = g_hWnd; swapChainDesc.SampleDesc.Count = 1; swapChainDesc.Windowed = TRUE; D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, NULL, 0, D3D11_SDK_VERSION, &swapChainDesc, &g_pSwapChain, &g_pDevice, NULL, &g_pDeviceContext); // 创建渲染目标视图 ID3D11RenderTargetView* pRenderTargetView = nullptr; ID3D11Texture2D* pBackBuffer = nullptr; g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); g_pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView); pBackBuffer->Release(); // 渲染循环 MSG msg; while (g_isRendering) { if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { // 清空渲染目标视图 float clearColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; // RGBA颜色,这里设置为透明色 g_pDeviceContext->ClearRenderTargetView(pRenderTargetView, clearColor); // 在这里添加你的渲染代码 // 呈现到屏幕 g_pSwapChain->Present(0, 0); } } // 释放资源 pRenderTargetView->Release(); g_pDeviceContext->Release(); g_pSwapChain->Release(); g_pDevice->Release(); // 销毁窗口 DestroyWindow(g_hWnd); UnregisterClass(wc.lpszClassName, wc.hInstance); return 0; } LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CLOSE: g_isRendering = false; // 关闭渲染循环 return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, message, wParam, lParam); } ``` 在这个示例代码中,我们增加了一个全局变量 `g_isRendering` 来控制渲染循环是否继续进行。当收到 `WM_CLOSE` 消息时,我们将 `g_isRendering` 设置为 `false`,从而关闭渲染循环,但程序仍然保持运行。 这样,你可以在其他地方通过调用 `PostMessage(g_hWnd, WM_CLOSE, 0, 0)` 来关闭渲染循环,而不会退出程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值