Creating an Activity and calling multiple Layouts can be harmful?

Asked

Viewed 969 times

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

}
  • 2

    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)

3 answers

4

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

From the outset because it goes against the first principle of SOLID: a class must have a single responsibility.
The name Activity(Activity) itself suggests this.

At each Activity must match a layout, with the logic to manipulate it.
That’s how you get:

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

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

In a simple situation, such as the question, you can even "give" to manage the code so that it doesn’t break due to changes in Layout, but with increasing complexity it becomes difficult.

If you want to have just one Activity use a Fragment for each of the layouts.

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

0

I don’t see how harmful.

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

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

Since they have their own life cycle.

  • Not harmful, but has problems, consider using Fragments. I didn’t know your position, should be used or not?

  • It is a question whether it will impact the application in any way... my answer is no! I just think that for the use he wants to make it I advise the implementation of Fragments.

  • Regarding the "problems" I consider because I do not know how it will encode in the other life cycles. Since all the layouts of it are in the onCreate().

0

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

An Fragment needs a "Manager" who will manage the transitions between one and the other, making there are no problems with memory for example.

In addition, you can separate your "Views" into several classes, thus keeping more organized.

If you want, you can research a little about the use of ViewPager (If you’re using Android Studio, create a "Tabedactivity" from its examples)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.