2016-11-21 56 views
0

我有一个片段,它有三个tabhosts。他们每个人都是自己的片段。让我们称他们为A,B,C。然后从片段CI使用startactivityonresult开始活动: -片段不更新listview

public class FavCommittee extends Fragment { 
    public ArrayList<Committee> favCommitteeData = new ArrayList<>(); 

    @Nullable 
    @Override 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     favCommitteeData.clear(); 
     View rootView = inflater.inflate(R.layout.house, container, false); 
     Context cont = getActivity(); 
     final SharedPreferences pref = getContext().getSharedPreferences("MyFav", 0); 
     final SharedPreferences.Editor editor = pref.edit(); 
     Map<String,?> entries = pref.getAll(); 
     final Set<String> keys = entries.keySet(); 
     int count=0; 
     for (String key:keys) { 
      String val = pref.getString(key, null); 
      String[] store = val.split(":"); 
      if (store[0].equals("committee")) 
       count=count+1; 
     } 
     int max=0; 
     for (String k:keys){ 
      String val = pref.getString(k,null); 
      if(val!=null) 
       max=Integer.parseInt(k); 
     } 
     for (int i=0;i<max+1;i++) { 
      String val = pref.getString(Integer.toString(i), null); 
      if (val != null) { 
       String[] store = val.split(":"); 
       if (store[0].equals("committee")) { 
        count = count - 1; 
        new GetAllCommittees(getContext(), rootView).execute(store[1], Integer.toString(count)); 
       } 
      } 
     }  final ListView yourListView = (ListView) rootView.findViewById(R.id.house_listview); 
     yourListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 
       Committee obj = (Committee)yourListView.getItemAtPosition(position); 
       Intent intent = new Intent(FavCommittee.this.getActivity(), committee_info.class); 
       intent.putExtra("Committee",obj.getCommittee_id()); 
       startActivityForResult(intent, 1); 
      } 
     }); 

     return rootView; 
    } 

在我关闭活动以下列方式活动: -

public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       onBackPressed(); 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
     setResult(Activity.RESULT_OK); 
     finish(); 
    } 

现在,在C片段再次我已经等实现onActivityResult()这个:

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     getActivity(); 
     if(requestCode == 1 && resultCode == Activity.RESULT_OK) { 
      Log.d("Called Again",Integer.toString(favCommitteeData.size())); 
     } 

事情onActivityResult在活动关闭后不会被调用。有人可以帮我理解为什么?

回答

0

我猜你没有在你的onActivityResultFragmentC上设置断点。问题是这样的:我基本上在我自己的项目中做了相同的情况,onActivityResult被调用,但resultCode是Activity.RESULT_CANCELLED。这是因为您的活动中您的已覆盖onBackPressed()仍然有super.onBackPressed(),并且将结果设置为CANCELLED,而不管之后会发生什么。

所以最简单的答案是从您的活动中重写的onBackPressed()方法中移除super.onBackPressed(),它应该会很好。

+0

没有活动......片段C只是在另一个片段里 – Anirban

+0

我其实发现了其他问题。我相应地编辑了我的答案。 – LukeWaggoner