这是我的Android应用程序的一个活动:现在我只是测试,回到另一个活动工作良好。问题是:当我点击后退按钮一切正常发生,但当我按后退箭头在左上角的屏幕在ActionBar,活动不进入onBackPressed()方法(所以其他活动崩溃,因为它期待一个意图通过)。对如何解决这个问题有什么想法吗?
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);
}
}
ActionBar
或Toolbar
上的反向箭头为HomeAsUp
按钮,在onOptionsItemSelected()
处理
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}