问题:
当我在第二个活动上按下智能手机和操作栏的后退按钮时,我有想要回调的主要活动。但它总是崩溃,当我放一个finish()时它才起作用;在主要活动中,但如果我这样做,那么智能手机的后退按钮将无法正常工作。
主要活动:
public class Principal extends SherlockActivity {
public static int THEME = R.style.Theme_Sherlock;
private Button entrar;
private Button cadastrar;
@Override
protected void onCreate(Bundle savedInstanceState) {
//setTheme(Principal.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
entrar = (Button)findViewById(R.id.entrar);
entrar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startActivity(new Intent(Principal.this,LoginActivity.class));
finish();
}
});
cadastrar = (Button)findViewById(R.id.cadastrar_home);
cadastrar.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(Principal.this, RegisterActivity.class);
startActivity(intent);
//finish();
}
});
}
第二活动:
public class RegisterActivity extends SherlockActivity{
protected void onCreate(Bundle savedInstanceState) {
setTheme(Principal.THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case android.R.id.home:
// Do whatever you want, e.g. finish()
Intent intent = new Intent(this, Principal.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
break;
}
return true;
}
}
已经尝试了很多方法,但都没有,只有finish();