ListFragment 中的 startActivityForResult 似乎没有调用 onActivityResult
我确实在这里尝试了“固定”jar:
http://code.google .com/p/android/issues/detail?id=15394
并完全重新安装了 SDK,但这两种方法都无法解决我在这里遇到的问题。那么 startActivityForResult
是否只是 ListFragment
的不可行呢?
原始帖子:
我有一个 ClientListView
,它是一个 ListFragment
,当单击操作栏上的按钮时,它会获取 ListFragment
中选择的内容> 查看并启动一个新活动来编辑选定的客户(或者如果单击其他选项,则一起创建一个新客户)。
这一切都启动得很好。 ClientListView
片段和 ClientDetailsFragment
被我的 EditClientActivity
FragmentActivity(它调用 ClientEdit
片段)替换。这会占据整个屏幕并在操作栏中创建一个保存/取消按钮。
问题是,当单击保存时,我无法使用新创建的客户端或编辑的客户端更新我的 ListFragment
。为了完整起见,这是我的调用顺序:
MainActivity FragmentActivity
设置 ClientListView ListFragment
和 ClientDetailsActivity FragmentActivity
(其中具有 ClientDetails
代码 > 片段)。然后,在选择新的或编辑客户端选项后,ClientListView ListFragment
可以在 EditClientActivity
上启动ActivityForResult(其中包含 ClientEdit
片段)。
ClientEdit
片段设置保存取消的选项菜单,一旦选择 ClientEdit
片段中的保存,就会发生几件事情:
新客户端或编辑后的客户端被保存到数据库中。 mEditListener.onEditComplete()
被调用。由于调用 FragmentActivity
EditClientActivity
实现了 onEditCompleteListener
,我在 ClientEdit
中使用 onAttach
分段。
那么我的 EditClientActivity
有 onEditComplete(long id)
方法:
public void onEditComplete(long id) {
Intent in = new Intent();
this.setResult(1, in); //just something to let the ClientListView that the client i saved refresh the list.
Toast.makeText(this.getBaseContext(), "Client Saved", Toast.LENGTH_LONG).show();
finish(); //go back to our listview and client details view
}
在我的 ClientListView
(类型为“ListFragment”)中,我有这个
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.setHasOptionsMenu(true);
//which columns to put as the cursor
String[] columns = new String[] { "firstname", "lastname" };
//how to post those columns into the layout. check client_row.xml for these ids
int[] to = new int[] { R.id.client_first_name_list_label, R.id.client_last_name_list_label};
myCursor = getClientsCursor(); //this is NOT closing the database connection if it does it gets an error
theClients = new SimpleCursorAdapter(this.getListView().getContext(),
R.layout.client_row, myCursor, columns, to);
setListAdapter(theClients);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.new_client:
// TODO: add recipe
showClientEdit(-1);
return true;
case R.id.client_delete:
// TODO: delete recipe
Toast.makeText(getActivity(), "Delete Client selected", Toast.LENGTH_LONG).show();
return true;
case R.id.client_edit:
if(mCurrentSelectedItemIndex!=-1)
showClientEdit(mCurrentSelectedItemIndex);
else
Toast.makeText(getActivity(), "Select client to edit!", Toast.LENGTH_LONG).show();
return true;
case android.R.id.home:
// TODO: Handle app icon click
Toast.makeText(getActivity(), "Home icon selected", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
void showClientEdit(long someId)
{
..do stuff to get the right client to pass to the intent
Intent intent = new Intent(getActivity(), EditClientActivity.class);
// Send the recipe index to the new activity
intent.putExtra(EditClientActivity.SELECTED_CLIENT, clientId);
startActivityForResult(intent, Activity.RESULT_OK);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
//never gets here :(
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this.getListView().getContext(), "Result code: " +resultCode , Toast.LENGTH_LONG).show();
if(resultCode ==1)
{
myCursor = getClientsCursor(); //not sure if i need this for the next line or not, want my list to update with newly
//added client or edited client names etc...
theClients.notifyDataSetChanged();
Toast.makeText(this.getListView().getContext(), "Data set notified!!!" , Toast.LENGTH_LONG).show();
}
}
:问题是我的 startActivityForRestult
从 ListFragment 调用 EditClientActivity
?据我所知,永远不会调用onActivityResult
。我提供了所有这些信息,试图弄清楚并掌握 Fragment/FragmentActivities 等应该如何相互交互。我是从我在教程、开发人员指南等中学到的东西来这样做的。我对自己的进步非常满意,但现在却陷入困境......并且可能意识到我做事的方式并不是正确的方式。 ..我很想得到启发。对我来说,Android 最难的部分是管理所有这些活动和视图如何相互作用......
I did try the "fixed" jar here:
http://code.google.com/p/android/issues/detail?id=15394
and also reinstalled the SDK completely and neither approach still fixed the issue I have here. So is startActivityForResult
just a no go from ListFragment
?
Original post:
I have this ClientListView
which is a ListFragment
, that when the button on the action bar is clicked it takes what is selected in the ListFragment
view and starts a new activity to edit the selected client (or if the other option is clicked a new client all together).
This all launches fine. The ClientListView
fragment and the ClientDetailsFragment
are replaced by my EditClientActivity
FragmentActivity (which calls the ClientEdit
fragment). This takes up the whole screen and creates a save/cancel button in the action bar.
The problem is that when the save is clicked, I cannot update my ListFragment
with the newly created client or edited client. For completeness this is my calling order:
MainActivity FragmentActivity
sets up the ClientListView ListFragment
and the ClientDetailsActivity FragmentActivity
(which has the ClientDetails
fragment). Then the ClientListView ListFragment
upon its new or edit client option being selected can startActivityForResult on the EditClientActivity
(Which has the ClientEdit
fragment in it).
The ClientEdit
Fragment sets up the options menu for save cancel, once the save in the ClientEdit
fragment is selected several things happen:
new client or edited client is saved to the database.mEditListener.onEditComplete()
is called. As the calling FragmentActivity
EditClientActivity
implements an onEditCompleteListener
that i use onAttach
in the ClientEdit
fragment.
So then my EditClientActivity
has the onEditComplete(long id)
method:
public void onEditComplete(long id) {
Intent in = new Intent();
this.setResult(1, in); //just something to let the ClientListView that the client i saved refresh the list.
Toast.makeText(this.getBaseContext(), "Client Saved", Toast.LENGTH_LONG).show();
finish(); //go back to our listview and client details view
}
In my ClientListView
(of type 'ListFragment') I have this:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.setHasOptionsMenu(true);
//which columns to put as the cursor
String[] columns = new String[] { "firstname", "lastname" };
//how to post those columns into the layout. check client_row.xml for these ids
int[] to = new int[] { R.id.client_first_name_list_label, R.id.client_last_name_list_label};
myCursor = getClientsCursor(); //this is NOT closing the database connection if it does it gets an error
theClients = new SimpleCursorAdapter(this.getListView().getContext(),
R.layout.client_row, myCursor, columns, to);
setListAdapter(theClients);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.new_client:
// TODO: add recipe
showClientEdit(-1);
return true;
case R.id.client_delete:
// TODO: delete recipe
Toast.makeText(getActivity(), "Delete Client selected", Toast.LENGTH_LONG).show();
return true;
case R.id.client_edit:
if(mCurrentSelectedItemIndex!=-1)
showClientEdit(mCurrentSelectedItemIndex);
else
Toast.makeText(getActivity(), "Select client to edit!", Toast.LENGTH_LONG).show();
return true;
case android.R.id.home:
// TODO: Handle app icon click
Toast.makeText(getActivity(), "Home icon selected", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
void showClientEdit(long someId)
{
..do stuff to get the right client to pass to the intent
Intent intent = new Intent(getActivity(), EditClientActivity.class);
// Send the recipe index to the new activity
intent.putExtra(EditClientActivity.SELECTED_CLIENT, clientId);
startActivityForResult(intent, Activity.RESULT_OK);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
//never gets here :(
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this.getListView().getContext(), "Result code: " +resultCode , Toast.LENGTH_LONG).show();
if(resultCode ==1)
{
myCursor = getClientsCursor(); //not sure if i need this for the next line or not, want my list to update with newly
//added client or edited client names etc...
theClients.notifyDataSetChanged();
Toast.makeText(this.getListView().getContext(), "Data set notified!!!" , Toast.LENGTH_LONG).show();
}
}
Is the issue is that my startActivityForRestult
calls the EditClientActivity
from a ListFragment? As far as I can tell never calls the onActivityResult
. I provided all this information to try to figure out and get a handle on how Fragment/FragmentActivities and the like are all supposed to interact with each other. I am doing it this way from things I learned poking around tutorials, the developer guide etc. I am pretty happy with my progress but at a wall now...and probably realizing the way I am doing things is just not the right way...I would love to be enlightened. This is the hardest part of android to me is managing how all these activities and views interact with each other....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否使用 Fragment 的兼容性库?
兼容性包存在问题,片段内的 onActivityResult 已损坏。看看这里 http://code.google.com/p/android /issues/detail?id=15394 。您还可以在那里下载固定版本的 jar 文件。
Are you using the compatibility library for Fragments?
There is an issue with the Compatibility package, onActivityResult within fragments is broken. Take a look here http://code.google.com/p/android/issues/detail?id=15394 . There you can also download a jar file with the fixed version.
我知道这个问题已经很老了,但我想为那些遇到这个问题的人提供一个解决方案,就像我在搜索为什么
onActivityResult
在Intent< 时没有运行时所做的那样。 /code> 从片段内部调用(
ListFragment
或常规Fragment
将具有相同的结果)。您必须从主 Activity 中调用
startActivityForResult
。因此,当您从片段内部调用 getActivity() 时,请确保调用 getActivity(),如下所示:I understand that this question is old, but I wanted to provide a solution for those that are hitting this question like I did when they were searching for why your
onActivityResult
is not running when theIntent
is called from inside a fragment (ListFragment
or regularFragment
will have the same result).You must call your
startActivityForResult
from your main Activity. So make sure to callgetActivity()
when you call it from inside a fragment like this:鬼鬼想到了这一点。这是我使用结果/请求代码的方式:
startActivityForResult(intent, Activity.RESULT_OK);
不是启动它的方式,在其中添加了另一个数字而不是
Activity.RESULT_OK
它起作用了,我一定很困惑。Freaking figured this out. It was the way I was using the Result/request code:
startActivityForResult(intent, Activity.RESULT_OK);
was not the way to start it, added another number in there instead of
Activity.RESULT_OK
and it worked, I must of gotten confused.