0

I am developing a simple app that has two activities, MainActivity and SecondActivity and a transparent CustomActivity that extends Dialog. The MainActivity has two buttons (Yes_button and No_button).

  • When user clicks Yes_button, the SecondActivity is called via Intent and the CustomActivity will be in front of it.

  • When user clicks No_button the SecondActivity will also be called but the CustomActivity will not be called alongside with it.

The calling of the CustomActivity is based on if-else statement expressions. The CustomActivity has a skip button, when clicked the CustomActivity will close only then can the SecondActivity be accessible to the user. The SecondActivity has just one button that calls the MainActivity and the cycle continues.

Problem

When the app launches and a user clicks on the No_button, the SecondActivity will be called without the CustomActivity (as expected!), but ONCE a user clicks on the Yes_button, the SecondActivity will keep on displaying alongside the CustomActivity EVEN when the No_button is clicked.

Expectation

I want the SecondActivity to be called alongside the CustomActivity each time the Yes_button is clicked and also let only the SecondActivity be called when ever the No_button is click.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xml:toos="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private static int getNumber;
    Button Yes_button;
    Button No_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Yes_button = findViewById(R.id.button1);
        No_button = findViewById(R.id.button2);
        Yes_button.setOnClickListener(this);
        No_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                int get_input = 1;  // will be use in if else statement.
                getNumber = get_input;
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                break;
            case R.id.button2:
                Intent intent2 = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent2);
                break;

        }
    }

    public static int get_Logic() {
        return getNumber;
    }
}

activity_second_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xml:toos="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:onClick="Click"
        android:text="Click" />

</RelativeLayout>

SecondActivity.java

public class SecondActivity extends AppCompatActivity {

    private static int output;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_activity);
        int received = MainActivity.get_Logic();
        output = received;
        Display();
    }

    public final void Display() {
        if (output == 1) {
            CustomActivity custom = new CustomActivity(SecondActivity.this);
            custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            custom.show();
        } else {
            CustomActivity custom = new CustomActivity(SecondActivity.this);
            custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            custom.cancel();
        }

    }

    public void Click(View view) {
        Intent intent = new Intent(SecondActivity.this, MainActivity.class);
        startActivity(intent);
    }
}

activity_custom_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="180dp"
    android:layout_height="250dp"
    xml:toos="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:text="Skip" />

</RelativeLayout>

CustomActivity.java

class CustomActivity extends Dialog {

    Button SkipButton;
    private Activity main;

    public CustomActivity(Activity constr) {
        super(constr);
        this.main = constr;
    }

    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setCancelable(false);
        setContentView(R.layout.activity_custom_activity);
        SkipButton = findViewById(R.id.button);
        SkipButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}

checking.xml

<?XML version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#FFFF" />
            <corners android:radius="20dp" />
            <stroke android:width="4dp" android:color="@color/colorPrimary" />
            <gradient />
        </shape>
    </item>
</selector>
Son Truong
  • 13,661
  • 5
  • 32
  • 58
Neldison
  • 48
  • 7

1 Answers1

0

In Android, to pass data from an activity to another, you don't need to declare a static variable (getNumber in MainActivity), you should use Intent and Bundle together. See the below solution.

MainActivity.java

public class MainActivity extends BaseActivity implements View.OnClickListener {

    Button Yes_button;
    Button No_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Yes_button = findViewById(R.id.button1);
        No_button = findViewById(R.id.button2);
        Yes_button.setOnClickListener(this);
        No_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int input = v.getId() == R.id.button1 ? 1 : 0;
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("input", input);
        startActivity(intent);
    }
}

SecondActivity.java

public class SecondActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_activity);
        Display();
    }

    public final void Display() {
        int input = getIntent().getIntExtra("input", 0);
        CustomActivity custom = new CustomActivity(SecondActivity.this);
        custom.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        if (input == 1) {
            custom.show();
        } else {
            custom.cancel();
        }
    }

    public void Click(View view) {
        finish();
    }
}
Son Truong
  • 13,661
  • 5
  • 32
  • 58
  • 1
    Awesome! it worked for me. I have been trying to solve this for some weeks now, but to no avail. I am really grateful that you helped me out. Could you please explain this line of codes for me? int input = v.getId() = = R.id.button1 ? 1:0; Especially this ? 1:0 – Neldison Nov 23 '20 at 16:42
  • It is the shorter version of your code. If id of the clicked button is R.id.button1 then return 1, otherwise (R.id.buttob2) return 0. Finally assign the value to the input variable. – Son Truong Nov 23 '20 at 23:14
  • How can I achieve this with three buttons. I tried doing this but it isn't working. in MainActivity button1, button2 and button3 int input2 = v.getId() == R.id.button1? 1:0; int input2 = v.getId() == R.id.button1? 2:0; Intent ok = new Intent(Main.this, Second.class); ok.putExtra("input1", input1); ok.putExtra("input2", input2); startActivity(ok); In Second Activity int Input1= getIntent().getIntExtra("input1", 0); int Input2= getIntent().getIntExtra("input2",0); if(input1==1){custom.show();} else if(input2==2){custom.Cancel();} else{Do Some other things} – Neldison Dec 12 '20 at 12:29
  • int input = 0; if (v.getId() == R.id.button1) {input = 1;} else if (v.getId() == R.id.button2) {input = 2;} else {input = 3;} – Son Truong Dec 12 '20 at 12:51
  • In SecondActivity, if input = 1, button1 clicked. if input = 2, button2 clicked. if input = 3, button3 clicked. – Son Truong Dec 12 '20 at 12:54
  • Thank you for your help, I really appreciate it. You talked about not using static variable to pass data. Well, I normally use it to pass data say from Activity A to Activity C. – Neldison Dec 12 '20 at 16:01