Building a Multi-Screen Android Game App with Activity Communication

Project Overview

This project demonstrates Android Activity navigation and inter-activity data exchange. The application consists of five screens: one main dashboard and four sub-screens accessible via image buttons.

Main Activity Implemantation

The entry point manages navigation to child activities and handles returning data.

MainActivity.java

package com.example.gameapp;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    
    private TextView scoreDisplay;
    private static final int REQ_INTRO = 100;
    private static final int REQ_GAME = 101;
    private static final int REQ_ARCHIVE = 102;
    private static final int REQ_CREDITS = 103;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        scoreDisplay = findViewById(R.id.score_text);
        
        ImageView introBtn = findViewById(R.id.btn_intro);
        ImageView playBtn = findViewById(R.id.btn_play);
        ImageView archiveBtn = findViewById(R.id.btn_archive);
        ImageView creditsBtn = findViewById(R.id.btn_credits);
        
        introBtn.setOnClickListener(v -> launchActivity(IntroActivity.class, REQ_INTRO));
        playBtn.setOnClickListener(v -> launchActivity(GameActivity.class, REQ_GAME));
        archiveBtn.setOnClickListener(v -> launchActivity(ArchiveActivity.class, REQ_ARCHIVE));
        creditsBtn.setOnClickListener(v -> launchActivity(CreditsActivity.class, REQ_CREDITS));
    }
    
    private void launchActivity(Class<?> activityClass, int requestCode) {
        startActivityForResult(new Intent(this, activityClass), requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            String result = data.getStringExtra("result_data");
            scoreDisplay.setText(result);
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_background"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="16dp">

    <ImageView
        android:id="@+id/btn_intro"
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:contentDescription="@string/game_intro"
        android:src="@drawable/btn_intro" />

    <ImageView
        android:id="@+id/btn_play"
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:layout_marginTop="24dp"
        android:contentDescription="@string/start_game"
        android:src="@drawable/btn_play" />

    <ImageView
        android:id="@+id/btn_archive"
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:layout_marginTop="24dp"
        android:contentDescription="@string/save_data"
        android:src="@drawable/btn_archive" />

    <ImageView
        android:id="@+id/btn_credits"
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:layout_marginTop="24dp"
        android:contentDescription="@string/credits"
        android:src="@drawable/btn_credits" />

    <TextView
        android:id="@+id/score_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:textSize="36sp"
        android:textColor="#FFFFFF" />
</LinearLayout>

Game Introduction Screen

Displays game information and returns control to the main screen.

IntroActivity.java

package com.example.gameapp;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class IntroActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro);
        
        View backButton = findViewById(R.id.btn_back);
        backButton.setOnClickListener(v -> returnWithData("Game Overview Screen"));
    }
    
    private void returnWithData(String message) {
        Intent result = new Intent();
        result.putExtra("result_data", message);
        setResult(RESULT_OK, result);
        finish();
    }
}

activity_intro.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/intro_bg">

    <TextView
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/tap_to_return"
        android:textColor="#FF4444"
        android:textSize="48sp" />
</FrameLayout>

Game Screen

Simulates a game session and returns the player's score.

GameActivity.java

package com.example.gameapp;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class GameActivity extends AppCompatActivity {
    
    private int playerScore = 3600;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        
        findViewById(R.id.exit_zone).setOnClickListener(v -> returnScore());
    }
    
    private void returnScore() {
        Intent result = new Intent();
        result.putExtra("result_data", "Final Score: " + playerScore);
        setResult(RESULT_OK, result);
        finish();
    }
}

activity_game.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/game_bg">

    <TextView
        android:id="@+id/exit_zone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/score_3600"
        android:textColor="#FF4444"
        android:textSize="48sp" />
</FrameLayout>

Archive Screen

Handles save file operations.

ArchiveActivity.java

package com.example.gameapp;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class ArchiveActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_archive);
        
        findViewById(R.id.confirm_btn).setOnClickListener(v -> {
            Intent result = new Intent();
            result.putExtra("result_data", "Save Archive: Complete");
            setResult(RESULT_OK, result);
            finish();
        });
    }
}

activity_archive.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/archive_bg">

    <TextView
        android:id="@+id/confirm_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/load_save"
        android:textColor="#FF4444"
        android:textSize="48sp" />
</FrameLayout>

Credits Screen

Displays development team information.

CreditsActivity.java

package com.example.gameapp;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class CreditsActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credits);
        
        findViewById(R.id.dismiss_btn).setOnClickListener(v -> {
            Intent result = new Intent();
            result.putExtra("result_data", "Development Team");
            setResult(RESULT_OK, result);
            finish();
        });
    }
}

activity_credits.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/dismiss_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/team_credits"
        android:textColor="#FF4444"
        android:textSize="48sp" />
</FrameLayout>

Key Implementation Details

Activity Result Handling

Use startActivityForResult() to launch child activities and retrieve data via onActivityResult(). The request code identifies which activity returned, while the result Intent carries the payload.

Data Passing Mechanism

Child activities create a Intent, attach data using putExtra(), set the result code to RESULT_OK, and call finish(). This triggers the parent's onActivityResult() callback with the trensmitted data.

Layout Structure

Each activity uses either LinearLayout with vertical orientation or FrameLayout as the root container. Background drawables provide visual styling while interactive elements use consistent sizing and spacing.

Tags: Android Activity Intent UI Development startActivityForResult

Posted on Tue, 19 May 2026 16:33:07 +0000 by smallflower