2016-11-08 8 views
0

私はタブ付きアクティビティにフラグメントを持っています。これはonclicklistenerからアクティビティにリダイレクトされます。そのアクティビティで変更が加えられました。そのアクティビティから戻った後に起動されるリスナーが必要です。ユーザーが変更を加えた後、ユーザーにそのフラグメントに戻ってフラグメントをリロードするボタンをユーザに与えます。どちらかがわかりません。 私はonResume()onStart()を試みましたが、そのトランザクションでフラグメント化されたことが何も起こらなかったため、すべてのことが機能しませんでした。 addOnBackStackChangedListener()を試しましたが、フラグメントではないアクティビティに配置する必要があります。 どうすればいいですか?あなたが内部のアクティビティを開くとそのアクティビティでバックプッシュした後にアクティビティに移動したリフレッシュフラグメント

:ユーザーが好きなよう項目を作り、私はそれがあなたのために働く願うボタンを背中押した後、あなたの時間のための 感謝

public static class PlaceholderFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          final Bundle savedInstanceState) { 
     final View rootView = inflater.inflate(R.layout.fragment_history, container, false); 
     getFragmentManager().addOnBackStackChangedListener(
       new FragmentManager.OnBackStackChangedListener() { 
        @Override 
        public void onBackStackChanged() { 
         getFragmentManager().beginTransaction().detach(getTargetFragment()).attach(getTargetFragment()).commit(); 
        } 
       }); 
     //TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
     //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 
     return rootView; 
    } 

    @Override 
    public void onStart() { 
     super.onResume(); 
     getFragmentManager().beginTransaction().detach(this).attach(this).commit(); 
    } 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getFragmentManager().beginTransaction().detach(this).attach(this).commit(); 
    } 

答えて

0

2つのアクティビティAとBの2つがあるとします。最初にf1が追加されます。f1 startアクティビティBの項目をstartActivityForResult(Intent intent、int requestCode)およびonActivityResult(int requestCode、int resultCode、Intent data)は、(可能であれば)フラグメントデータを更新するか、フラグメントをリロードします。

0

iは、リストビューのために同じ問題を抱えていましたフラグメントは、それが一時停止に行くだろうし、あなたがボタンを押すと戻るonResumeが、私はこのようにもう一度onResumeメソッドで私のメソッドを呼び出したので、実行されます。

@Override 
public void onResume() { 
    super.onResume(); 
    storedfavoritesitem = FavoriteModel.getAllFavorites(); 
    if (storedfavoritesitem.size() == 0) { 
     nolist.setVisibility(View.VISIBLE); 
     nolist.setText("Tu Lista de Favoritos está vacía ;)"); 
    } 
    if (adapter!=null && currentLocation!=null) { 
     mResualt.clear(); 
     for (int i = 0; i < storedfavoritesitem.size(); i++) { 
      FavoriteModel Faveitem = storedfavoritesitem.get(i); 
      String locality = Faveitem.name; 
      String cityname = Faveitem.city; 
      int id = Faveitem.id; 
      double lat = Faveitem.lat; 
      double lon = Faveitem.lon; 
      Location location = new Location("Beach"); 
      location.setLatitude(lat); 
      location.setLongitude(lon); 
      float distance = currentLocation.distanceTo(location); 
      mResualt.add(new SearchResualtClass(id, distance/1000, locality, cityname)); 
     } 
     adapter.notifyDataSetChanged(); 
    } 
} 

歓声!

関連する問題