2
As I am still walking in the study of Android applications and read things in this regard, I wonder if, for example, I have only one Activity and several layouts being called from it, there is a risk that the application is not in accordance with good practices.
In the code below I have the main layout of Activity and two more being called. In these two additional, there are buttons that allow me to change the screen for either one or another additional layout.
There is also an audio, which starts when I change the screen from the main layout to the activity_a layout, and receives the stop when access to the activity_b layout.
This can be harmful in some aspect?
public class MainActivity extends AppCompatActivity {
private Button btn_iniciar;
private RelativeLayout activity_a, activity_b;
private MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CarregarTelaPrincipal();
}
public void CarregarTelaPrincipal() {
setContentView(R.layout.activity_main);
btn_iniciar = (Button) findViewById(R.id.btn_iniciar);
btn_iniciar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CarregarLetraA();
}
});
}
public void CarregarLetraA() {
setContentView(R.layout.activity_a);
activity_a = (LinearLayout) findViewById(R.id.activity_a);
player = MediaPlayer.create(this, R.raw.keyboard);
player.start();
activity_a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
player.stop();
CarregarLetraB();
}
});
}
public void CarregarLetraB() {
setContentView(R.layout.activity_b);
activity_b = (LinearLayout) findViewById(R.id.activity_b);
activity_b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CarregarTelaPrincipal();
}
});
}
}
Unnecessary complexity. Extra concern for nonsense. If you’re going to use the onBackPressed method (which is called when you click the < "back" button on android), for example, then you’ll have to keep checking and checking. If you need to go from an A screen to a B screen, you will have to keep inventing animations to give the feeling of screen exchange (because the screen will not be changed). Sometimes it is necessary to keep one screen active while another overlays it and then back... You would be unable to start an Activity expecting a result (startActivityForResult)
– Mr_Anderson