How can I make a button that calls another screen, and on the other screen have a button that when I click, return to the home screen?
How can I make a button that calls another screen, and on the other screen have a button that when I click, return to the home screen?
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 Screen 2 class:
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 any button that will have the same result. What happens above is that you start a new Activity , but the previous one remains in Stack , that is, it has not been finalized, only paused. Then, giving finish , it reopens because it has remained in the queue for activities .
Feel free to ask new questions in the comment for this answer, should you have any questions.