按下操作栏或内置后退按钮时如何启动 Intent
How to start Intent when either actionbar or builtin back button is pressed
所以我有一个意图,我想在按下两个后退按钮中的任何一个时开始。这个后退按钮 "getSupportActionBar().setDisplayHomeAsUpEnabled(true);" 或设备内置的所有 android 手机上的那个。我四处搜索,找不到解决方案。
这是我的代码,您可以在 if 语句中看到我想要 "if back button is pressed" 代码。我感谢任何意见!
CheckBox routeCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
//back button for route details view
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//sets actionbar title
routeName = getIntent().getExtras().getString("routeName");
getSupportActionBar().setTitle(routeName);
///////checkbox///////
routeCheckBox = (CheckBox) findViewById(R.id.routeCheckBox);
routeCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
if (routeCheckBox.isChecked())
{
Intent check = new Intent(RouteDetails.this,MainActivity.class);
check.putExtra("checkImageResource", R.drawable.checkmark);
/////////////////// ////////////
if ( actionbar back button or the built in backbutton on all android devices is pressed)
{
startActivity(check);
}
////////////////////////////////
}
}
});
新代码编辑:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
// your code here
routeCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
if (routeCheckBox.isChecked())
{
Intent check = new Intent(RouteDetails.this,MainActivity.class);
check.putExtra("checkImageResource", R.drawable.checkmark);
startActivity(check);
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
// your code here
}
所以我有一个意图,我想在按下两个后退按钮中的任何一个时开始。这个后退按钮 "getSupportActionBar().setDisplayHomeAsUpEnabled(true);" 或设备内置的所有 android 手机上的那个。我四处搜索,找不到解决方案。
这是我的代码,您可以在 if 语句中看到我想要 "if back button is pressed" 代码。我感谢任何意见!
CheckBox routeCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_details);
//back button for route details view
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//sets actionbar title
routeName = getIntent().getExtras().getString("routeName");
getSupportActionBar().setTitle(routeName);
///////checkbox///////
routeCheckBox = (CheckBox) findViewById(R.id.routeCheckBox);
routeCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
if (routeCheckBox.isChecked())
{
Intent check = new Intent(RouteDetails.this,MainActivity.class);
check.putExtra("checkImageResource", R.drawable.checkmark);
/////////////////// ////////////
if ( actionbar back button or the built in backbutton on all android devices is pressed)
{
startActivity(check);
}
////////////////////////////////
}
}
});
新代码编辑:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
// your code here
routeCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
if (routeCheckBox.isChecked())
{
Intent check = new Intent(RouteDetails.this,MainActivity.class);
check.putExtra("checkImageResource", R.drawable.checkmark);
startActivity(check);
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
// your code here
}