A
A
Alexey2019-08-02 19:32:11
Java
Alexey, 2019-08-02 19:32:11

How is it more correct from a logical and aesthetic point of view to open a new Activity?

1 option:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        Button firstButton = (Button) findViewById(R.id.firstButton);
        firstButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Context context = view.getContext();
                Intent intent = new Intent(context, secondActivity.class);
                context.startActivity(intent);
            }
        });
        ...
    }

Option 2:
public void buttonClick(View view){
        Intent intent = new Intent(this, secondActivity.class);
        startActivity(intent);
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
EVGENY T., 2019-08-02
@zeroWiNNeR

The second option has a margin for the future - if you need to call activities from different places, perform some additional actions, then the code will be more concise in the end.
PS In a good way, the Intent for the call should be returned by the called activity.

D
Denis Zagaevsky, 2019-08-03
@zagayevskiy

From an aesthetic and logical point of view, it is correct to create a Router class, which will contain the openSomeScreen () method, and which will be called from all the right places.
You most likely don't need two activities, you need fragments.
The activity/fragment code should contain the necessary minimum of actions.

T
terminator-light, 2019-08-04
@terminator-light

I agree with Denis Zagaevsky . After all, it is considered good form to have one activity and many fragments. Google IO 2019 showed Navigation Architecture Components and encouraged to use Single Activity, because Activity is an entry point, and it should be one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question