1

I am working on an android application in which I am using an activity as a custom dialog.I've named my custom dialog activity as Dialog_activity and my game activity as Activity1. In Dialog_activity, there are two buttons namely yes and no. The dialog asks the user if he/she wants to start a new game. So, how can I call a method from Activity1 in Dialog_activity in the OnClick method of the yes button. This is a tic tac toe application. Here is the code:

Activity1

public class Dialog_activity extends Activity {
Button yesbutton,nobutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
Button btn4 = (Button) findViewById(R.id.button4);
Button btn5 = (Button) findViewById(R.id.button5);
Button btn6 = (Button) findViewById(R.id.button6);
Button btn7 = (Button) findViewById(R.id.button7);
Button btn8 = (Button) findViewById(R.id.button8);
Button btn9 = (Button) findViewById(R.id.button9);
}

public void resetButtons()
{
btn1.setText("");
btn2.setText("");
btn3.setText("");
btn4.setText("");
btn5.setText("");
btn6.setText("");
btn7.setText("");
btn8.setText("");
btn9.setText("");
}
}

Dialog Activity

public class Dialog_activity extends Activity {
Button yesbutton,nobutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    setContentView(R.layout.dialog_activity);
    Intent startdialog = getIntent();
    yesbutton = (Button) findViewById(R.id.button);
    nobutton = (Button) findViewById(R.id.button2);
    nobutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    yesbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            //call the resetButtons() method
        }
    });

Thanks!

Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63

4 Answers4

3

Here are few options:

  1. You can make reset button() as static but for that you will also have to make the buttons static.This method is not recommended.

  2. Use this How to create a Custom Dialog box in android? to create a custom dialog and inflate this custom dialog in your Activity1 only .You can make CustomDialogClass as an inner class and thus it will be able to access all Activity1 methods.

Hope it helps.

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • So, calling a method in an activity from another activity is not possible? – Chinmay Dabke Nov 20 '13 at 05:55
  • You can by using point 1 as stated .But creating static variables unless its crucial is not recommended because http://stackoverflow.com/questions/8563843/static-variable-loses-value – Rachita Nanda Nov 20 '13 at 06:03
  • 1
    I recommend to not maintain any View in android with static reference as Context is associated with every View and which can leads to context leakages – Gopal Gopi Nov 20 '13 at 06:10
2

Make instance of Dialog_activity in Activity1 using the instance call the method which you want.

Class Activity1 extends Activity
{

   Dialog_activity dialog_activity;

    btn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                dialog_activity.the_method_you_want;

            }
        });
}
Ajay
  • 552
  • 2
  • 4
  • 16
0

There two ways I can suggest,

1) start the Dialog activity from Game activity for result by using startActivityForResult() and override onActivityResult() in GameActivity

2) Register GameActivity to listen for the Custom Intent action by using registerReceiver()and broadcast that action in DialogActivity by using sendBroadCast()

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
0

1. import activity_1 2. write class variable Dialog_activity activity1 inside below class 3. call reset method using this activity1 reference activity1.resetButtons(); inside yesbutton onclocklistener

1. import activity_1

public class Dialog_activity extends Activity {
Button yesbutton,nobutton;

2. write class variable Dialog_activity activity1 inside below class

Dialog_activity activity1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    setContentView(R.layout.dialog_activity);
    Intent startdialog = getIntent();
    yesbutton = (Button) findViewById(R.id.button);
    nobutton = (Button) findViewById(R.id.button2);
    nobutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    yesbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {`enter code here`
            finish();
            **//call the resetButtons() method using reference activity1 
            activity1.resetButtons();
        }
    });
Antesh Sharma
  • 309
  • 2
  • 3