我希望有人可以帮助解决我的问题。Android后退按钮在某些活动中有效,而在其他活动中则被破坏。我的 MainActivity 有其他目标活动可以去,这取决于选择的选项。当我的应用程序管理地图时,当我单击标记的信息窗口时,它会根据需要转到详细活动,当我单击 Android 后退按钮时,它会正常返回主活动(不要与操作栏中的主页按钮,我不想实现)。
但是,当我点击“联系人”或“条款和条件”选项时,我打算像使用 infowindows 一样启动联系人/条款活动,它会像往常一样打开活动,但后退按钮不会t 工作,它看起来有点重新打开 Contact/Terms 活动并且没有回到主要活动。我搜索了很多帖子,并添加了我的观察结果:
- 我不在我的 MainActivity 上调用 finish(),所以我不是故意弄乱活动堆栈。
- 正如这篇文章所建议的那样,我已经尝试了 putExtra("finishActivityOnSaveCompleted", true) ,但没有运气。
- 我已经尝试使这两个活动相同(详细信息和联系人活动),只显示一个简单的布局,其中的导入不超过 os.bundle、app.Activity、content.Intent、view.View 和 View.Window。
- 我已经尝试触发 startActivity() 函数,该函数从覆盖某些函数的方法调用 Contact Activity(使用覆盖点击事件的方法的 startActivity 调用详细的 Acvitity)。
- 我已经尝试将一些额外的数据从 Main 放入/接收到 Contact 活动(这就是我的绝望)。
我的选项用完了,我不知道出了什么问题,而且我知道我不应该自己实现后退按钮功能,因为 Android 会为我处理它。我在清单中针对 SDK 版本 16 及更高版本,我添加了调用这两个活动的代码的简短版本:
public class MainActivity extends FragmentActivity implements OnNavigationListener, ClusterManager.OnClusterClickListener<MyItem>, ClusterManager.OnClusterInfoWindowClickListener<MyItem>, ClusterManager.OnClusterItemClickListener<MyItem>, ClusterManager.OnClusterItemInfoWindowClickListener<MyItem>
{
.
.
.
/*Some variables initialized*/
.
.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
.
.
.
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
menuOpt=position;
getData();
//gridView.setVisibility(View.GONE);
if(gridView.getVisibility() == View.VISIBLE) {
toggleView(gridView);
}
}
});
}
public void getData(){
String optString="";
llMatVesp.setVisibility(View.GONE);
switch(menuOpt){
/* In this case the back button to MainActivity works, I manage action bar operations for a particular behaviour of the menu, all options in the switch do */
case 1:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setCustomView(null);
getResults();
break;
/* In this case the back button of Contact Activity, that's supposed to return to MainActivity doesn't work */
case 2:
Intent conIntent=new Intent(MainActivity.this, Contact.class); startActivity(conIntent);
break;
default:break;
}
}
/*this function brings data from db, add markers to the map, and animates to a certain point
public void getResults(){
.
.
}
/* This functions extract the right data from the marker and make the intent that starts the detail Activity, where the back button works with no problems */
@Override
public void onClusterItemInfoWindowClick(MyItem item) {
showDetail(item.getId(),item.getMenuOpt());
}
public void showDetail(String id, int menuOpt){
Intent showDetail=new Intent(MainActivity.this, ShowDetail.class);
showDetail.putExtra("menuOpt", menuOpt);
showDetail.putExtra("icon", ic);
showDetail.putExtra("id", id);
startActivity(showDetail);
}
}
联系活动有一个表单并发送电子邮件,但返回按钮不起作用的另一个活动,TermsAndConditions 活动,只显示一个简单的线性布局,就是这样,那里没有花哨的编码。