Posts tagged ‘activity’

April 15th, 2010

Run multiple activity instances in Android from the single same class

Here is a bit of software design, this post is no aimed at GMAT folks. Only you happen to be interested in Android development
The GMAT is a set of questions of the same type but with different content . In “Data Sufficiency” type, the 5 radiobutton answers stay the same. Only  the question above and the right answer (among the 5 choices) change.

Lately I tried to run pages (“activity” in android terminology). What I want to achieve is the “page naviguation” through the re-use of the same class. In the android framework , a layout is associated with one activity which is inherited by a class. I don’t want to manage as many layouts/class as many questions. I want to break this triple layout/class/activity .
After some search I found this link
http://stackoverflow.com/questions/2298816/how-to-create-multiple-instances-of-an-activity

Here is the ‘magic code’. In the class “demo”, the method newActivity calls itself ( sort of recursive ).

public void newActivity (){
    	Intent i = new Intent(this, demo.class);
      	i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      	startActivity(i);
    }

After selecting the right answer, the user has to confirm by click on Next button to get the next question. In the flow, when the user clicks on the button, another activity is called.

setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.Next);
btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            	newActivity(); // this method contains the intent to go to the next question

            }

        });

To verify we access to the next activity, I implemented a static “count” incrementing for new activity. The complete code source should look like

package com.gmat;

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

// http://stackoverflow.com/questions/2298816/how-to-create-multiple-instances-of-an-activity

public class demo extends Activity {
	private static int count;
	public String[] questions = { "Is x an integer ?\n(1) x/2 is an integer \n(2) 2x is an integer",
			"What is the value of x ? \n(1)2x + 1=0 \n(2) (x+1)²=x²",
			"What is the value of 1/k + 1/r ? \n(1) k+r = 20 \n(2) kr = 64" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView quest = (TextView) findViewById(R.id.question);
        Button btn = (Button) findViewById(R.id.Next);

        if (count<3){
        	quest.setText(count + "  " + questions[count]);
        }
        else{
        	quest.setText(count + " questions " );
        }

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            	newActivity();

            }

        });

    }

    public void newActivity (){
    	Intent i = new Intent();
 		i.setClass(this, demo.class);
      	i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      	startActivity(i);
      	count++;
    }
}

Here below is the layout of “Data Sufficiency” type and its tricks.
The answer are  radiobuttons which are themselves groupped in a radiogroup .
Notice the inner RelativeLayout inside the main LinearLayout make the tricks of alignment.
Learn about this
http://stackoverflow.com/questions/2386866/how-to-align-views-at-the-bottom-of-the-screen

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView
		android:id="@+id/question"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content"
	    android:text="@string/question"
	    android:textSize="17dip"
	    android:textStyle="bold"
	    />

	<RelativeLayout
    android:id="@+id/InnerRelativeLayoutQuestions"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" >

		<RadioGroup android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:orientation="vertical"
		android:id="@+id/radiogroup1">

			<RadioButton android:text="@string/data_suf_a"
				android:id="@+id/Rep1"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:textSize="@dimen/txtdatasuf">
			</RadioButton>
			<RadioButton android:text="@string/data_suf_b"
				android:id="@+id/Rep2"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:textSize="@dimen/txtdatasuf">
			</RadioButton>
			<RadioButton android:text="@string/data_suf_c"
				android:id="@+id/Rep3"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:textSize="@dimen/txtdatasuf">
			</RadioButton>
			<RadioButton android:text="@string/data_suf_d"
				android:id="@+id/Rep4"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:textSize="@dimen/txtdatasuf">
			</RadioButton>
			<RadioButton android:text="@string/data_suf_e"
				android:id="@+id/Rep5"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:textSize="@dimen/txtdatasuf">
			</RadioButton>
		 </RadioGroup>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/InnerRelativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

		<Button android:text="Next"
			android:id="@+id/Next"
			android:layout_alignParentRight="true"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content">
		</Button>

    </RelativeLayout>

</RelativeLayout>