0

this is my first time trying to make an android app, or dealing with java, so please be patient with my questions :(

what i'm trying to do is to have screen-1 with a few buttons, when pressed, it will bring the user to screen-2 with another group of buttons. upon pressing a button in screen-2, the user will be brought to a new screen with some text based on which buttons he pressed, and in which order.

same goes with the other buttons in each screen.

what i already have is screen 1 full of buttons, and their ids. I'm not sure where to even begin for the next part. I can create an onClick activity for every button in screen-1, but how will screen-3 remember which buttons were pressed on screen-1 and screen-2?

activity_main.xml

<Button 
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button1"
            android:onClick="button1OnClick"/>
                    <Button 
                    android:id="@+id/button2"
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button2"
                    android:onClick="button1OnClick2"/>

MainActivity.java

public class Click extends Activity {
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main.xml);
        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // open up screen 2? do i use intent??
        }
    });
}

}

activity_main2.xml

<Button 
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button1"
            android:onClick="button1OnClick"/>
                    <Button 
                    android:id="@+id/button2"
            android:layout_height="wrap_content"
            android:layout_width="110dp"
            android:layout_weight="1"
            android:text="@string/button2"
                    android:onClick="button1OnClick2"/>

MainActivity2.java

public class Click extends Activity {
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main2.xml);
        final Button button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // open up screen 3?  
        }
    });
}

}

screen-1 and screen-2 will share the same set of buttons. screen 3 will show which order, and which buttons were pressed.

user2498691
  • 25
  • 1
  • 2
  • 5

2 Answers2

2

You need to learn how to pass data and parameters via intents.

Take a look at this:

How do I pass data between Activities in Android application?

Community
  • 1
  • 1
0

You pass that information along in the intent when you start the next activity.

eg in your protected void onCreate(Bundle savedInstanceState) you'll do something like:

Bundle extras = getIntent().getExtras();
if (extras != null) {
   // test for the arg you passed in and extract value eg
   String foostr = extras.getString(FOO_STRING);
   int fooint = extras.getInt(FOO_INT);
}

and in the caller that's starting the activity,

Intent intent = new Intent(this, Screen-2.class);
intent.putExtra(FOO_INT,myInt);
intent.putExtra(FOO_STRING,myString);
startActivity(intent);
K5 User
  • 606
  • 1
  • 6
  • 10