Maybe I don't quite understand Activities & Intents...

Started by
1 comment, last by frob 10 years, 11 months ago

Hello, So I am trying to put an activity that displays a picture in the beginning of my app for 2 seconds and then goes to another activity where the user tries to guess the computers number. The MainActivity works fine, Problem is, when I add the new activity (splash) to the manifest file, and change the splash activity as the launcher and the main activity to default for the intent category, it doesnt run anymore! Can someone pleeease help me understand what I am doing wrong...

MainActivity.java


package com.bwall.thenumbergame;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;

public class MainActivity extends Activity {
	
	// global variables
	int number, compNumber;
	EditText textField;
	TextView result, gameObjective;
	Button button;
	Random rand = new Random(System.nanoTime());
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// setting views
		textField = (EditText) findViewById(R.id.textField);
		result = (TextView) findViewById(R.id.resultView);
		gameObjective = (TextView) findViewById(R.id.gameObjective);
		button = (Button) findViewById(R.id.submitButton);
		
		// creating computer number
		compNumber = rand.nextInt(10);
		    
		button.setOnClickListener(new View.OnClickListener() {
				
			@Override
			public void onClick(View v) {
				number = Integer.parseInt(textField.getText().toString());
					
				// checks if correct guess
				if(number == compNumber) {
					result.setText("CORRECT! The nummber was " + compNumber);
				} else {
					result.setText("LOL nope. Guess Again.");
				}
			}
		});
	}
}

Splash.java


package com.bwall.thenumbergame;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class Splash extends Activity {
	
	final Context context = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		
		Thread timer = new Thread() {
			public void run() {
				
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					Intent mainIntent = new Intent(context, MainActivity.class);
					startActivity(mainIntent);
				}
			}
		};
		timer.start();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}
}

splash.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/splashbackground"
    android:orientation="vertical" >

</LinearLayout>

activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/gameObjective"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="@string/gameObjective"
        android:textSize="30sp" />

    <EditText
        android:id="@+id/textField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gameObjective"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:ems="10"
        android:inputType="number" />

    <requestFocus />

    <Button
        android:id="@+id/submitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textField"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="@string/submitButton" />

    <TextView
        android:id="@+id/resultView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/submitButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="58dp"
        android:textIsSelectable="true" />

</RelativeLayout>

TheNumberGame Manifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bwall.thenumbergame"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.bwall.thenumbergame.Splash"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="com.bwall.thenumbergame.SPLASH" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.bwall.thenumbergame.MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Advertisement

You still have MainActivity defined as your MAIN intent. Read Intent.

That aside, Splash Screen are EVIL. Do not use them.

Michael A. - Software Engineer, moonlighting as a game developer
A Brief History of Rome
Pirates and Traders

Your game should only have a single activity and intent.

Some apps may have multiple intents. For example, you may open a map application to run it, or you may use it for navigation, or you may want to open a street view. That would be three intents for a single application.

For playing a game you really have only one intent. A user won't probably say "I want to look up my address book contact using this game", or say "I want to open this pdf file using the game", or "I want to navigate to an address using the game". Those would be different intents for the application. They almost never apply to games.

If you decide to use a splash screen (deep sigh, see Strategy's post) then it should be as part of your game. Although I detest splash screens, I do understand that they are sometimes there for corporate branding requirements, and other times they are useful to hide the fact that you are loading resources. In the second case they are a necessary evil.

Whatever your reason for it, your splash screen should simply be your initial game state. You start in the "showing splash screen" state. Then when the time expires you transition to your main menu state.

On the other hand..... If you did decide to make your splash screen into its own application with its own intent, it will be blissfully easy to swap it out with a non-splash-screen empty app. So in that case your users might be happy for your action since it means they can trivially bypass or remove your splash screen.

This topic is closed to new replies.

Advertisement