gpt4 book ai didi

安卓 onBackPressed() 不工作

转载 作者:行者123 更新时间:2023-11-30 02:51:38 28 4
gpt4 key购买 nike

我正在尝试添加此功能:

-当点击 Return 按钮时,它会弹出一个退出提示 (AlertDialog with yes or no)

问题

但是它不能正常工作。如果我点击按钮,它会直接退出程序。

这是我的整个 Activity 代码:(我测试过的退出函数在代码的末尾)

@SuppressWarnings("deprecation")
public class MainScreen extends TabActivity {
// TabSpec Names
private static final String Alerts_SPEC = "Alertes";
private static final String Status_SPEC = "Status";
private static final String Details_SPEC = "Events";

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);

TabHost tabHost = getTabHost();

// Inbox Tab
TabSpec alertsSpec = tabHost.newTabSpec(Alerts_SPEC);
alertsSpec.setIndicator(Alerts_SPEC);
Intent alertsIntent = new Intent(this, FragmentAlerts.class);
alertsSpec.setContent(alertsIntent);

// Outbox Tab
TabSpec statusSpec = tabHost.newTabSpec(Status_SPEC);
statusSpec.setIndicator(Status_SPEC);
Intent statusIntent = new Intent(this, FragmentStatus.class);
statusSpec.setContent(statusIntent);

// Profile Tab
TabSpec detailsSpec = tabHost.newTabSpec(Details_SPEC);
detailsSpec.setIndicator(Details_SPEC);
Intent detailsIntent = new Intent(this, FragmentEvents.class);
detailsSpec.setContent(detailsIntent);


// Adding all TabSpec to TabHost
tabHost.addTab(alertsSpec); // Adding Inbox tab
tabHost.addTab(statusSpec); // Adding Outbox tab
tabHost.addTab(detailsSpec); // Adding Profile tab


}

public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);

return super.onCreateOptionsMenu(menu);
}

/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh
finish();
startActivity(getIntent());
return true;
case R.id.action_password:
// help action
Intent i = new Intent(getApplicationContext(), LoginActivityAdmin.class);
startActivity(i);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// -------------- EXIT CODE BEGINS ------------------------------

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){

if ((keyCode == KeyEvent.KEYCODE_BACK)) { //stop your music here
// To exit application
onBackPressed2();
}
return super.onKeyDown(keyCode, event);
}

public void onBackPressed2() {

new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainScreen.this.finish();
}
})
.setNegativeButton("No", null)
.show();

}

}

解决方案:

我为每个 child 添加了这段代码:

@Override
public void onBackPressed() {
this.getParent().onBackPressed();
}

然后在我的主要 Activity 中调用它:

    @Override 
public void onBackPressed() {

new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainScreen.this.finish();
}
})
.setNegativeButton("No", null)
.show();

}

最佳答案

从这个链接尝试一些东西:

Key Events in TabActivities?

每个选项卡的 Activity 处理“后退”按键。

关于安卓 onBackPressed() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24055545/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com