2

Im making a simple Androidgame , where the user selects an answer to a question (ind Activity1) by clicking a radiobutton. When the correct radiobutton is clicked, a button in the "Credits" (Activity2) will get VISIBLE and be available for the user.

How can I make this happen? i can´t get the two activities to work together?

The code from Activity 1 (the Question) where the user clicks the radiobutton:

 final Button s1 = (Button) findViewById(R.id.radio0);
 final Button s2 = (Button) findViewById(R.id.radio1);
 final Button s3 = (Button) findViewById(R.id.radio2);

 s1.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) {
      btnEliminar.setVisibility(View.VISIBLE);
      btnKort.setVisibility(View.VISIBLE);
      s1.setVisibility(View.GONE);
      s2.setVisibility(View.GONE);
      s3.setVisibility(View.GONE);




      AlertDialog.Builder builder = new AlertDialog.Builder(Activity1.this);
        builder.setMessage("...");
        builder.setCancelable(true);
        builder.setPositiveButton("...", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();

                }
            });

        AlertDialog alert = builder.create();
        alert.show();

        }

  });

The code from Activity2 where the button should get visible:

public class Activity2 extends Activity {

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);

        Button credit1 = (Button) findViewById(R.id.buttoncredit1);
        credit1.setVisibility(View.INVISIBLE);
        ....
        credit1.setVisibility(View.VISIBLE);

Hope someone is able to help me Thank you

user1154161
  • 23
  • 1
  • 3

5 Answers5

6

This can be done via Intent's extras. Should look something like this:

//Somewhere in Activity1
Intent intent = new Intent();
intent.setClass(getApplicationContext(), Activity2.class);
intent.putExtra("makeButtonVisible", true); // Or false
startActivity(intent);

//Somewhere in Activity2
boolean isButtonVisible = getIntent().getBooleanExtra("makeButtonVisible");
// Change button's visibility accordingly
Ash
  • 1,621
  • 13
  • 17
2

To understand the best answer to this question you must understand that each Activity in Android is essentially isolated from any other activities. This means you cannot change things such as widget visibility from one activity to another.

That said, the best solution is likely to pass extras using an Intent when you start up your 2nd activity. That can be done quite easily by following these steps.

I hope this helps!

Community
  • 1
  • 1
Codeman
  • 11,457
  • 8
  • 48
  • 89
1

You can do it by different ways:

1) Implement static method in Activity2, and call it from Activity1. Don't change visibility directly - just change some static field of Activity2 and then handle it in onStart()

2) When starting Activity2, put values into Intent and then handle them in Activity2. But keep in mind, that user can use "Back" button, so you need to handle it correctly (for example, you can .finish() activity and then start new one).

First one may sounds easier, but try to avoid such static methods. Second way is better for your app design.

Dmitry Zaytsev
  • 22,662
  • 13
  • 89
  • 139
  • 1
    you shouldn't try to control one activity from another. This is against Android's application model. You should simply pass a "true/false" intent. – Codeman Jan 17 '12 at 15:17
1

In Activity1

    s1.setOnClickListener(new OnClickListener() { 
          public void onClick(View arg0) {

    Intent intent = new Intent(this,Activity2.class);

    if(s1.isChecked)
        intent.putExtra("state",0);
    else
        intent.putExtra("state",-1);

    startActivity(intent);

  });

In Activity 2

Intent intent = getIntent();
int state = Integer.parseInt(intent.getExtras().get("state").toString());
credit1.setVisibility(state);

Hope this help

Pandy
  • 145
  • 1
  • 9
0

you should use Intent for change a activity to an other activity:

Intent intent = new Intent(thisClss.this, AnOtherClass.class);
startActivity(intent);
Omid Nazifi
  • 5,165
  • 8
  • 28
  • 55