如何处理后退按钮时startActivityForResult有效? [英] How do I handle the back button when startActivityForResult is active?

查看:100
本文介绍了如何处理后退按钮时startActivityForResult有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的形式,其中一个用户可以从列表中添加,编辑和删除的人。当用户选择编辑一个人就执行startActivityForResult,因此可以进行相应的修改和刷新列表,一旦编辑完成。如果用户点击,从出现一个强制关闭错误的编辑屏幕的后退按钮。

I have a simple form where a user can add, edit, and delete people from a list. When a user has chosen to edit a person it executes startActivityForResult so it can make the appropriate changes and refresh the list once the edit is complete. If the user clicks the back button from the edit screen a force close error appears.

我相信这是与系统预期的结果,而我没有捕获它正确。我将如何捕获这种错误?

I believe it has something to do with the system expecting a result, and I'm not trapping it properly. How would I trap this error?

下面是目前onActivityResult code的地方:

Here is the onActivityResult code currently in place:


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
 super.onActivityResult(requestCode, resultCode, intent);
 Bundle extras = intent.getExtras();
 switch(requestCode) {
 case ACTIVITY_CREATE:
  String person = extras.getString("person");
  mDbHelper.addPerson(person);
  fillData();
  break;
 case ACTIVITY_EDIT:
  Long rowId = extras.getLong("_id");
  if (rowId != null) {
   String editPerson = extras.getString("person");
   mDbHelper.updatePerson(rowId, editPerson);
  }
  fillData();
  break;
 }
}

感谢您的任何帮助。

推荐答案

首先,看看使用DDMS的堆栈跟踪,它会告诉你什么是行唯一的例外是发生在。

First of all, look at your stack trace using DDMS, it will tell you what line the Exception is occurring on.

什么您可以在您电话的活动做的是检查是否有结果code ,并在被叫活动设置使用<一个href="http://developer.android.com/reference/android/app/Activity.html#setResult%28int%29"><$c$c>setResult().

What you can do in your calling activity is check for the resultCode, and in your callee activity set it using setResult().

例如,如果用户pressed回结果code将<一href="http://developer.android.com/reference/android/app/Activity.html#RESULT_CANCELED">RESULT_CANCELED.如果是这样的情况下,不要尝试从意图提取数据。

For example, if the user pressed back the resultCode will be RESULT_CANCELED. If this is the case do not try to extract data from the intent.

这篇关于如何处理后退按钮时startActivityForResult有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆