Reputation: 1711
This is an Activity of my Android App: for now I'm just testing that the going back to another activity works well. The problem is: when I hit the back button everything happens normally, but when I press the back arrow in the top left of the screen in the ActionBar, the activity doesn't enter the onBackPressed() method (and so the other activity crashes because it's expecting an intent passed). Any ideas on how to solve this?
package com.example.thefe.newsmartkedex;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
/**
* Created by TheFe on 20/10/2016.
*/
public class MyPokeDetails extends AppCompatActivity {
int pokeID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_poke_details);
Intent j = getIntent();
pokeID = j.getExtras().getInt("id");
getActionBar();
Button back = (Button)findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (getApplicationContext(), PokemonDetails.class);
i.putExtra("id", pokeID);
startActivity(i);
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent (getApplicationContext(), PokemonDetails.class);
i.putExtra("id", pokeID);
startActivity(i);
}
}
Upvotes: 1
Views: 337
Reputation: 1780
The back arrow on the ActionBar
or Toolbar
is the HomeAsUp
button and is handled in onOptionsItemSelected()
.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Upvotes: 6