Esse menu aparece nas duas activity, segue abaixo o código da activity ListaAlunos.
package br.com.alura.agenda;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class ListaAlunosActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String[] alunos = {"Eduardo","Jeferson","Rodrigo"};
ListView listaAlunos = (ListView) findViewById(R.id.lista_alunos);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, alunos);
listaAlunos.setAdapter(adapter);
Button novoAluno = (Button) findViewById(R.id.novo_aluno);
novoAluno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentVaiProFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
startActivity(intentVaiProFormulario);
}
});
}
}
Segue o código da activity do formulário.
package br.com.alura.agenda;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.zip.Inflater;
public class FormularioActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formulario);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_formulario, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_formulario_ok:
Toast.makeText(this, "O Aluno foi salvo com sucesso!", Toast.LENGTH_SHORT).show();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
};