我试图从android studio的意图中获得结果.
在我的主要内容中,我启动一个活动并使用startActivityForResult(intent,1)
然后我在活动2的setResults()中使用mainActivity中的get结果
@OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if(resultCode == RESulT_OK){ Bundle extras = data.getExtras(); if (extras != null) { String name = extras.getString("FirsTname"); String Lname = extras.getString("LASTname"); int ID = extras.getInt("ID"); //Todo: Get the List fragment to newinstance with out new arrayList Person p = new Person(name, Lname, ID); people.add(p); getFragmentManager().beginTransaction().replace(R.ID.content_main, FullList.newInstance(people)).commit(); }
在我的活动1中的片段中,我正在调用一个新的startActivityForResult(i,2)
如何让我的主要活动获取setResults()
活动3?
活动3正在这样做:
Intent deleteIntent = new Intent();deleteIntent.putExtra("Fname", first);deleteIntent.putExtra("Lname", last);deleteIntent.putExtra("ID", num);setResult(RESulT_OK, deleteIntent);finish();
我试图让我的主要活动调用if(requestCode == 2)
但它无济于事.
以下是所有onActivityResult供参考:
@OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if(resultCode == RESulT_OK){ Bundle extras = data.getExtras(); if (extras != null) { String name = extras.getString("FirsTname"); String Lname = extras.getString("LASTname"); int ID = extras.getInt("ID"); //Todo: Get the List fragment to newinstance with out new arrayList Person p = new Person(name, Lname, ID); people.add(p); getFragmentManager().beginTransaction().replace(R.ID.content_main, FullList.newInstance(people)).commit(); } // Now SEEING IF THE DETAILS SCREEN PASSED BACK RESulTS } else if (requestCode == 2) { if (resultCode == RESulT_OK) { Bundle extras = data.getExtras(); if (extras != null) { String name = extras.getString("Fname"); String Lname = extras.getString("Lname"); int ID = extras.getInt("ID"); Person p = new Person(name, Lname, ID); // Delete happens here // if (people.contains(p)) { people.remove(p); // If empty show blank frag, if not, update List // if (people.isEmpty()) { getFragmentManager().beginTransaction().replace(R.ID.content_main, BlankList.newInstance()); } else { getFragmentManager().beginTransaction().replace(R.ID.content_main, FullList.newInstance(people)).commit(); } } else { Toast.makeText(this, "DIDNT RECEIVE SAME INFO", Toast.LENGTH_SHORT).show(); } } } } // END ELSE CHECK }}
下面是在活动1的Fragment中调用startActivityForResult()的代码.
@OverrIDepublic voID onListItemClick(ListVIEw l, VIEw v, int position, long ID) { //super.onListItemClick(l, v, position, ID); ArrayList<Person> people = (ArrayList<Person>) getArguments().getSerializable(ARG_People); if (people != null && position != -1) { Person ListPerson = people.get(position); Intent i = new Intent("OPENDETAILS"); i.putExtra("name", ListPerson.name); i.putExtra("LASTname", ListPerson.lname); i.putExtra("ID", ListPerson.ID); startActivityForResult(i, 2); } else { Toast.makeText(getActivity(), "EMPTY List ERROR", Toast.LENGTH_SHORT).show(); }}
解决方法:
有点不清楚你要做什么,但听起来像是:
> Activity1为结果启动Activity2
> Activity2为结果启动Activity3
> Activity3返回结果
> Activity1预计会收到Activity3的结果.
如果我做对了,那么这里似乎缺少的关键元素就是你期望Activity1从Activity3获得一个结果,即使它是Activity2为结果启动它.在这种情况下,您应该在Activity2中实现onActivityResult,处理从Activity3返回的结果并将它们设置为Activity2的结果以传递回Activity1然后完成;活动仅接收通过startActivityForResult直接启动的活动的结果.
总结以上是内存溢出为你收集整理的android – 如何处理来自多个活动的getResults?全部内容,希望文章能够帮你解决android – 如何处理来自多个活动的getResults?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)