Create an Activity and call multiple Layouts can be harmful?

2

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();
        }
    });
}

}
    
asked by anonymous 27.10.2016 / 15:03

3 answers

4

Yes, it is "harmful" and should not use.

First of all because it goes against the first principle of the SOLID : a class must have only one responsibility. The name Activity itself suggests this.

Each Activity should match a layout , with its logic to handle it. With this you can:

  • Reduce code complexity.
  • Increased readability.
  • Coupling reduction.
  • Clean and testable code.
  • Ease of evolution.

Remember that the findViewById() method only finds the views that are within the layout indicated in the setContentView() method.

In a simple situation, as is the example of the question, you can even "give" to manage the code so that it does not break due to the Layout changes, but with increasing complexity this makes It's difficult.

If you just want an Activity use a Fragment for each of the layouts .

Isolating the code that handles each layout in a new class (Fragment) distributes the responsibilities, leaving only the Activity to handle when presenting each of them.

    
27.10.2016 / 15:29
0

I do not see how harmful.

You may have problems if you need to implement onStop() or onPause() in this same class as Activity .

However, for this feature, you have already considered using Fragments ?

Since they have their own life cycle.

    
27.10.2016 / 15:32
0

The correct would be to use Fragments , fragments are classes that have a Layout and need an Activity to work.

A fragment needs a "Manager" that will manage the transitions between one and another, causing no problems with memory for example.

In addition, you can separate your views into several classes, thus keeping you organized.

If you want, you can do a little research on using ViewPager (If you are using Android Studio, create a "TabedActivity" from the examples of it)

    
27.10.2016 / 15:34