ViewPager Activity to notify a Fragment of a specific event

后端 未结 6 1475
无人及你
无人及你 2020-12-08 02:59

I have a ViewPager and it is using a FragmentAdapter in order to display several fragments of the same kind. Although these Fragments are basicall

相关标签:
6条回答
  • 2020-12-08 03:16

    I don't know enough of what you are doing, but it sounds like you need to use an Observer pattern, Callbacks, or Listeners. Can't your fragment just do somthing like:

    myservice.addMyEventListener(myFragInstance);
    

    and then you can be "notified of a specific event."

    0 讨论(0)
  • 2020-12-08 03:17

    Have you tried FragmentManager.findFragmentByTag()

    FragmentManager manager = getSupportedFragmentManager();
    //with support package, else
    //FragmentManager manager = getFragmentManager()
    Fragment fragment = manager.findFragmentByTag("Tag You Created the Fragment");
    if (fragment instanceof Fragment1){
        Fragment1 fr = (Fragment1)fragment
        fr.updateData(DATA)
        //or any method of your choice
    } 
    

    EDIT: I read carefully! The instanceOf will cast a Fragment into your Fragment class. It was you, who suggested Fragment1 as a name for simpicity. Also, you didn't provide any source to help us. It is true, that you cannot set a Fragment's tag, but why do you think you are able to get its tag? Usually a Fragment is added through FragmentManagers like

    FragmentManager manager = getSupportedFragmnentManager()
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(int containerViewId, Fragment fragment, String tag);
    // or transaction.add(Fragment fragment, String tag)
    // ...other transactions
    transaction.commit()
    

    EDIT2: it's very easy though. according to your code you could just call

    Fragment fragment = mAdapter.getItem(0) // 0||1||2
    

    You should consider reading the docs (i.e about FragmenPagerAdapter) and post your source code so we don't have to guess what you need.

    0 讨论(0)
  • 2020-12-08 03:22

    Just look at this answer https://stackoverflow.com/a/16388650

    He has used yourAdapter.notifyDataSetChanged() which is a predefined method. Check the link to see how its done

    Edit: When your AsyncTask is done, you should do something like this onPostExecute method:

    ResultFragment resultFrag = (ResultFragment) getSupportFragmentManager()
                                     .findFragmentByTag("FragToRefresh");
    if (resultFrag != null) {
        resultFrag.refreshData(refreshedArray);
    }
    

    And in your ResultFragment you need to have refreshData method, which is something like this:

    public void refreshData(ArrayList<YourObject> data) {
       yourArray = new ArrayList<YourObject>(data);
       yourAdapter.notifyDataSetChanged();
    }
    
    0 讨论(0)
  • 2020-12-08 03:26

    Had to use event bus to make everything simple https://github.com/greenrobot/EventBus

    0 讨论(0)
  • 2020-12-08 03:28

    I had the same issue but fixed it with a localBroadcastReceiver like this:

    Create a receiver in your activity and register it:

     /**
     * ******************************
     * Receiver to process the message
     * *******************************
     */
    private BroadcastReceiver onNotice = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //You can send any extra data if you need like this
            final int type = intent.getIntExtra("fragment.data", -1);
            Log.d(tag, "main class: " + type);
            //also refresh your fragment like this
            mViewPager.getViewPager().getAdapter().notifyDataSetChanged();
        }
    };    
    
    @Override
    protected void onResume() {
        super.onResume();
    
        //Register a localbroadCast with the your filter
        IntentFilter thinaireFilter = new IntentFilter("your.filter");
        LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, thinaireFilter);
    }
    

    Remember to remove LocalBroadCast

    //remove the LocalBroadCast when no need it
    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice);
    }
    

    Send your broadcast from anywhere you want Adapters, services, etc.

    Intent sendBroadCastData = new Intent("your.filter");
    sendBroadCastData.putExtra("fragment.data", myData);
    LocalBroadcastManager.getInstance(context).sendBroadcast(sendBroadCastData);
    

    Hope it helps others.

    0 讨论(0)
  • 2020-12-08 03:35

    Try this,

    Register a broadcast receiver in all your fragments... like this

    create a class which extends a broadcast receiver in all the classes, for eg:

    public class FragmentReceiver1 extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
            }    
    }
    

    and register this receiver in you fragment's onCreate ...

    for eg. getActivity().registerReceiver(new FragmentReceiver1(), new IntentFilter("fragmentupdater"));

    Now assign a unique id to each of you fragment like 1 for Fragment1, 2 for Fragment2 and likewise

    now whenever you want to pass any data and update any of the fragment just send a broadcast with the data in intent and "fragmentupdater" as the intent-filter...

    For eg:

    Intent data = new Intent("fragmentupdater");
    data.putString("key","data");
    data.putInt("fragmentno",1); // Pass the unique id of fragment we talked abt earlier
    activity.sendBroadcast(data);
    

    Now each of your fragment will receive the data but you can verify if the data if for the same fragment by the unique id we passed in it in the onReceive function..., the intent which you get, is the intent we passed above

    0 讨论(0)
提交回复
热议问题