As I'm still struggling 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 will not agree with best practices.
In the code below I have the main layout of Activity and two more being called. In these two additional ones, there are buttons that allow me to switch the screen to either one or another additional layout.
There is still an audio, which starts when I change the main layout screen to the activity_a layout, and receives the stop when accessing the layout activity_b.
Can this be harmful in some way?
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();
}
});
}
}