2017-10-05 27 views
0

我試圖從一個活動發送數據到一個片段。 我沒有將數據從片段發送到活動。除了在活動中實例化接口偵聽器對象之外,我已經正確設置了一切。從活動發送數據到帶有接口監聽器的片段

public class Activity extends AppCompatActivity { 

    private FragmentInterface fragmentInterfaceListener; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // This line below is actually in a button onClick() 
    fragmentInterfaceListener.sendDataMethod(dataToSend); 

    } 

    public interface FragmentInterface { 
    void sendDataMethod(SampleData sampleData); 
    } 
    } 

然後在該片段中,我有:

public static class CustomFragment extends Fragment implements Activity.FragmentInterface { 

    @Override 
    public void sendDataMethod(final SampleData sampleData) { 

    }  
} 

當我把一個日誌行的按鈕onClick(),單擊該按鈕時出現的日誌行。不,我不打算將sampleData放入片段包中。是的,我需要通過一個界面發送數據。那麼如何正確實例化活動中的fragmentInterfaceListener對象?我是否缺少Activity或CustomFragment中的其他內容?

+0

你要問清楚。不要懶惰地把你的代碼 – zihadrizkyef

+1

而不是你在你的活動中保留'FragmentInterface'接口,爲什麼你不把'CustomFragment'片段保留在你的活動中。然後,您可以在'CustomFragment'片段中聲明公共方法,以便您的活動可以輕鬆使用這些方法。 –

回答

1

這裏缺少的是註冊部分。

的片段具有自身與活動偵聽活動註冊發送數據時,事件occurs.To爲此在活動

private void setOnDataListener(FragmentInterface interface){ 
    fragmentInterfaceListener=interface; 
} 

而在你的片段的OnCreate設置監聽器創建的方法這樣

((YOUR_ACTIVITY_NAME)getActivity()).setOnDataListener(this); 
+0

你最受歡迎的..如果這個工作,然後接受我的答案,並關閉這個問題 – Anonymous

0

你不需要使用監聽器的碎片,因爲你可以直接與該片從主機活動通信。

正如@ lq-gioan所說,您可以在您的Fragment中創建一個公共方法,然後從您的活動中調用它。因此,創建一個公共的方法來設置數據,這樣的事情:

public static class CustomFragment extends Fragment { 

    // public method to be accessed by host activity. 
    public void sendDataMethod(final SampleData sampleData) { 

    }  
} 

然後,你可以打電話給你的主活動中的方法:

CustomFragment fragment = (CustomFragment)getSupportFragmentManager() 
          .findFragmentById(R.id.fragment_id); 

// or use find by tag if you adding the fragment by tag 
// CustomFragment fragment = (CustomFragment)getSupportFragmentManager() 
//       .findFragmentByTag("FragmentTag"); 

// now you can call it 
fragment.sendDataMethod(yourSampleData); 
0

根據活動將數據發送到碎片,我們穿上」 t需要一個接口。

您可以直接調用片段的方法或通過如setArguments捆綁

 ArticleFragment articleFrag = (ArticleFragment) 
      getSupportFragmentManager().findFragmentById(R.id.article_fragment); 

    if (articleFrag != null) { 
     // If article frag is available, we're in two-pane layout... 

     // Call a method in the ArticleFragment to update its content 
     articleFrag.updateArticleView(position); 
    } else { 
     // Otherwise, we're in the one-pane layout and must swap frags... 

     // Create fragment and give it an argument for the selected article 
     ArticleFragment newFragment = new ArticleFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ArticleFragment.ARG_POSITION, position); 
     newFragment.setArguments(args); 

     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

     // Replace whatever is in the fragment_container view with this fragment, 
     // and add the transaction to the back stack so the user can navigate back 
     transaction.replace(R.id.fragment_container, newFragment); 
     transaction.addToBackStack(null); 

     // Commit the transaction 
     transaction.commit(); 
    } 

你可以參考https://developer.android.com/training/basics/fragments/communicating.html