2017-10-05 10 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); 
    } 
    } 

はその後の断片では、私が持っている:私はボタンonClick()にログ行を入れると、ボタンがクリックされたとき

public static class CustomFragment extends Fragment implements Activity.FragmentInterface { 

    @Override 
    public void sendDataMethod(final SampleData sampleData) { 

    }  
} 

、ログ行が表示されます。いいえ、私はフラグメントバンドルにsampleDataを入れるつもりはありません。はい、私はインターフェイスを介してデータを送信する必要があります。では、アクティビティでfragmentInterfaceListenerオブジェクトを正しくインスタンス化するにはどうすればよいですか?アクティビティやCustomFragmentに他のものがありませんか?

+0

を参照することができフラグメントでメソッドを呼び出すか、バンドル

で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(); } 

を渡すことができます。あなたのコードを置くのは怠惰ではありません – zihadrizkyef

+1

'FragmentInterface'インターフェースをあなたのアクティビティに保つのではなく、代わりにあなたのアクティビティに' CustomFragment'フラグメントを残してください。そして、 'CustomFragment'フラグメントでパブリックメソッドを宣言して、あなたのアクティビティがこれらのメソッドを簡単に使用できるようにします。 –

答えて

1

ここで欠けているのは登録部分です。

断片が、ときにイベントがこの活動

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

そしてリスナーを設定し、あなたのフラグメントのOnCreateの中でメソッドを作成するのですoccurs.Toデータを送信するための活動のための活動のリスナーに自身を登録する必要がありますこの

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

あなたの大歓迎..これがうまくいくなら、私の答えを受け入れ、この質問を閉じる – Anonymous

0

のようにあなたが直接そのホスト活動からのフラグメントと通信することができますので、フラグメントでリスナーを使用する必要はありません。

@ lq-gioanによると、あなたのフラグメントにパブリックメソッドを作成してから、それをあなたのアクティビティから呼び出すことができます。私たちは「ドンフラグメントにActivityからデータを送信するための

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

public static class CustomFragment extends Fragment { 

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

    }  
} 

は、その後、あなたのホストの活動の中にメソッドを呼び出すことができますので、データ、このような何かを設定するには、パブリックメソッドを作成しますインタフェースが必要です。

あなたは直接、あなたが明確に依頼する必要がありhttps://developer.android.com/training/basics/fragments/communicating.html

関連する問題