The life circle of Activity in andrid

package com.codecademy.myactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override // Bundle savedInstanceState save all of our activities on screen
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Toast is used to show a message on the screen with some time
        // when you use toast, you need to call show() method to show it, remember
        Toast.makeText(MainActivity.this, "OnCreate Called", Toast.LENGTH_SHORT).show();
    }

    // the life circle of activity
    @Override
    protected void onStart() {
        super.onStart();
        
        Toast.makeText(MainActivity.this, "OnStart Called", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        Toast.makeText(MainActivity.this, "OnPostResume Called", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Toast.makeText(MainActivity.this, "OnStop Called", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(MainActivity.this, "OnDestroy Called", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Toast.makeText(MainActivity.this, "OnPause Called", Toast.LENGTH_SHORT).show();
    }
    
    // Result:
    // onCreate() -> onStart() -> onPostResume() -> onPause() -> onStop() -> onDestroy()
}
以下是一个简单的C++实现Game of Life的示例代码: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int ROWS = 20; const int COLS = 60; // 初始化格子 void initializeGrid(bool grid[][COLS]) { srand(time(NULL)); for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { int random = rand() % 10; if (random < 3) { grid[row][col] = true; } else { grid[row][col] = false; } } } } // 显示格子 void displayGrid(bool grid[][COLS]) { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if (grid[row][col]) { cout << "*"; } else { cout << " "; } } cout << endl; } } // 计算每个格子周围活细胞的数量 int countNeighbors(bool grid[][COLS], int row, int col) { int count = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int neighborRow = row + i; int neighborCol = col + j; if (neighborRow >= 0 && neighborRow < ROWS && neighborCol >= 0 && neighborCol < COLS) { if (i != 0 || j != 0) { if (grid[neighborRow][neighborCol]) { count++; } } } } } return count; } // 更新格子 void updateGrid(bool grid[][COLS]) { bool newGrid[ROWS][COLS]; for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { int neighbors = countNeighbors(grid, row, col); if (grid[row][col]) { if (neighbors == 2 || neighbors == 3) { newGrid[row][col] = true; } else { newGrid[row][col] = false; } } else { if (neighbors == 3) { newGrid[row][col] = true; } else { newGrid[row][col] = false; } } } } for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { grid[row][col] = newGrid[row][col]; } } } // 主函数 int main() { bool grid[ROWS][COLS]; initializeGrid(grid); while (true) { system("cls"); // 清屏 displayGrid(grid); updateGrid(grid); } return 0; } ``` 以上代码使用二维布尔数组来表示格子的状态,并且使用函数来完成初始化、显示、计算和更新等操作。在主函数中,我们先使用 `initializeGrid` 函数来随机初始化格子的状态,然后使用 `displayGrid` 函数来显示格子,最后使用 `updateGrid` 函数来更新格子的状态。由于Game of Life是一个无限大的世界,因此我们在这里使用了一个简单的循环来模拟它。 需要注意的是,由于在Windows系统中,清屏需要使用 `system("cls")` 命令,因此以上代码只能在Windows系统中运行。如果你想在其他操作系统中运行该代码,需要使用相应的清屏命令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值