Button that calls another screen and another button on the new screen that goes back to the old screen

Asked

Viewed 849 times

-1

How can I make a button that calls another screen, and on the other screen have a button that when you click, goes back to the home screen?

1 answer

1


Screen 1

final Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(View.OnClickListener {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(this@Tela1, Tela2.class);
        startActivity(i);
    }
});

Screen 2

You can use any button or even set a navigation button on your Toolbar.

 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#fff"
            app:navigationIcon="@drawable/ic_back" /> <!-- este é o icone de navegação -->

And in the class of Screen 2:

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        finish();
    }
});

You can also use a button whatever the result will be the same. What happens above is that you start a new Activity, but the previous one continues in the Stack, that is, it was not finished, only paused. So, when giving Finish, she is reopened as she has remained in the line of activities.

Feel free to ask new questions in the comment of this answer if there are any questions.

  • I’m having a problem with screen code 1: Intent i = new Intent(this, Proxpasso.class); Cannot resolve constructor

  • You are using the above code within a Fragment?

  • I’m using inside the onCreate

  • Okay, try the following: Intent i = new Intent(getApplicationContext(), ProxPasso.class); and tell me if it resolves.

  • Stop the application from working

  • Right. Do you know where the Androidmanifest.xml file is? And if so, the Activity Proxpasso file is set in this file?

  • No, but I just copied it from Screen 1 and it worked, but I have an error on Screen 2, setSupportActionBar(Toolbar); cannot be Applied to (android.widget.Toolbar)

  • It is because the reference is incorrect. It must be android.support.v7.widget.Toolbar instead of Toolbar

  • Look for some import that leads to Toolbar and swaps for android.support.v7.widget.Toolbar and your problem will be solved. And if in your layout is Toolbar, switch to android.support.v7.widget.Toolbar as in the response code.

Show 4 more comments

Browser other questions tagged

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