'If statement to start second activity causing flashing screen bug
Flashing Screen Bug (please click here to view)
Hi I am trying to stop my app from flashing its second activity screen when I call the if condition you see in the GIF. Initially the values are set to null and once the app starts, if at least one of the conditions are met, it will go straight to the second activity. This is causing a weird flashing screen bug. Please help.
package com.example.assignment1;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
protected Button settings_button = null; //import and declare settings_button
protected TextView total_count; //objects used to locate in project so that is why we refactor ids
protected Button button_A = null;
protected Button button_B = null;
protected Button button_C = null;
protected Button show_my_counts = null;
private ArrayList<String> event;
private ArrayList<String> num;
int A=0;
int B=0;
int C=0;
int count=0;
int max_count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI(); //call setup_ui within on create
}
protected void setupUI() {
total_count = findViewById(R.id.total_count);
settings_button = findViewById(R.id.settings_button);
settings_button.setOnClickListener(onClickSettingsButton);
button_A = (Button) findViewById(R.id.button_A);
button_A.setOnClickListener(onClickButton_A);
button_B = (Button) findViewById(R.id.button_B);
button_B.setOnClickListener(onClickButton_B);
button_C = (Button) findViewById(R.id.button_C);
button_C.setOnClickListener(onClickButton_C);
show_my_counts = findViewById(R.id.show_my_counts);
show_my_counts.setOnClickListener(onClickSHOWCOUNTS);
total_count.setText("Total Counts: " + counter());
}
public int counter() //counter is updated with every button click as the value will increase
{
button_A.setEnabled(true); //ENABLE COUNTER UNTIL MAX IS MET
button_B.setEnabled(true);
button_C.setEnabled(true);
for (int i=0;i<=max_count;i++)
{
count = (A+B+C);
if(count==max_count) {
max_count=0;
button_A.setEnabled(false);
button_B.setEnabled(false);
button_C.setEnabled(false);
}
}
return count;
}
private final View.OnClickListener onClickSettingsButton= new Button.OnClickListener() {
@Override
public void onClick(View view) {
settings_button.setText("SETTINGS");
openActivity_settings();
}
};
protected void openActivity_settings(){ //open settings class on click
Intent i = new Intent(this, SettingsActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
}
private final View.OnClickListener onClickButton_A = new Button.OnClickListener()
{
@Override
public void onClick(View view) {
A++;
total_count.setText("Total Counts: "+ counter());
}
};
private final View.OnClickListener onClickButton_B = new Button.OnClickListener()
{
@Override
public void onClick(View view) {
B++;
total_count.setText("Total Counts: "+ counter());
}
};
private final View.OnClickListener onClickButton_C = new Button.OnClickListener()
{
@Override
public void onClick(View view) {
C++;
total_count.setText("Total Counts: "+ counter());
}
};
private final View.OnClickListener onClickSHOWCOUNTS = view -> {
show_my_counts.setText("SHOW MY COUNTS");
openActivity_data();
};
public void openActivity_data(){ //open data class on click
Intent intent = new Intent(MainActivity.this, dataActivity.class);
intent.putExtra("countA",String.valueOf(button_A));
intent.putExtra("countB",String.valueOf(button_B));
intent.putExtra("countC",String.valueOf(button_C));
intent.putExtra("totalCount",String.valueOf(total_count));
intent.putStringArrayListExtra("event_list",event);
intent.putStringArrayListExtra("num_list",num);
startActivity(intent);
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.ButtonName), Context.MODE_PRIVATE);
String Alp = sharedPreferences.getString(getString(R.string.button_nameA), null); //if name is null we don't have it stored in file
String Bet = sharedPreferences.getString(getString(R.string.button_nameB), null);
String Cet = sharedPreferences.getString(getString(R.string.button_nameC), null);
String Max = sharedPreferences.getString(getString(R.string.MAX), null);
if(Alp == null)
{
openActivity_settings();
}
else
max_count = Integer.parseInt(Max);
button_A.setText(Alp);
button_B.setText(Bet);
button_C.setText(Cet);
counter();
}
Second activity
package com.example.assignment1;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
public class SettingsActivity extends MainActivity {
private static final String TAG = null;
protected TextView counter_1_name;
protected TextView counter_2_name;
protected TextView counter_3_name;
protected TextView Max_count;
protected EditText edit_1;
protected EditText edit_2;
protected EditText edit_3;
protected EditText max_c;
protected Button save_button = null;
// public static final String SHARED_PREFS = "sharedPrefs";
// public static final String TEXT1 = "Counter 1 Name";
// public static final String TEXT2 = "Counter 2 Name";
// public static final String TEXT3 = "Counter 3 Name";
// public static final String TEXT4 = "Max Count";
//private String text1;
// private String text2;
//private String text3;
// private String text4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
set_UI();
}
protected void set_UI() {
save_button = findViewById(R.id.save);
save_button.setOnClickListener(onClickSAVE);
counter_1_name = findViewById(R.id.counter_1_name);
counter_2_name = findViewById(R.id.counter_2_name);
counter_3_name = findViewById(R.id.counter_3_name);
Max_count = findViewById(R.id.max_counts);
edit_1 = findViewById(R.id.edit1);
edit_2 = findViewById(R.id.edit2);
edit_3 = findViewById(R.id.edit3);
max_c = findViewById(R.id.max);
};
private final View.OnClickListener onClickSAVE= new Button.OnClickListener() {
@Override
public void onClick(View view) {
counter_1_name.setText(edit_1.getText().toString());
counter_2_name.setText(edit_2.getText().toString());
counter_3_name.setText(edit_3.getText().toString());
Max_count.setText(max_c.getText().toString());
// saveData();
{
String bA = edit_1.getText().toString();
String bB = edit_2.getText().toString();
String bC = edit_3.getText().toString();
String M = max_c.getText().toString().trim();
save_button.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();
SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.ButtonName), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.button_nameA), bA);
editor.putString(getString(R.string.button_nameB), bB);
editor.putString(getString(R.string.button_nameC), bC);
editor.putString(getString(R.string.MAX), M);
editor.apply();
}
}
};
@Override
public boolean onCreateOptionsMenu (Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dropdown, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.edit:
Toast.makeText(getApplicationContext(), "Editor Mode Active", Toast.LENGTH_SHORT).show();
save_button.setVisibility(View.VISIBLE);
return true;
}
return super.onOptionsItemSelected(item);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
