从活动发送数据到带有接口监听器的片段

问题描述:

我试图从一个活动发送数据到一个片段。 我没有将数据从片段发送到活动。除了在活动中实例化接口侦听器对象之外,我已经正确设置了一切。从活动发送数据到带有接口监听器的片段

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'片段中声明公共方法,以便您的活动可以轻松使用这些方法。 –

这里缺少的是注册部分。

的片段具有自身与活动侦听活动注册发送数据时,事件occurs.To为此在活动

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

而在你的片段的OnCreate设置监听器创建的方法这样

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

你最受欢迎的..如果这个工作,然后接受我的答案,并关闭这个问题 – Anonymous

你不需要使用监听器的碎片,因为你可以直接与该片从主机活动通信。

正如@ 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); 

根据活动将数据发送到碎片,我们穿上” 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