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>
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
SecondActivity.java