I have Fragment activity with pager:
List<Fragment> fragments = new Vector<Fragment>(); fragments.add(Fragment.instantiate(this, PastEventListFragment.class.getName(),bundle)); fragments.add(Fragment.instantiate(this, EventListFragment.class.getName(),bundle)); this.mPagerAdapter = new EventPagerAdapter(super.getSupportFragmentManager(), fragments); // ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager1); pager.setAdapter(this.mPagerAdapter); pager.setCurrentItem(1);
I catch onKeyDown event :
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { } return super.onKeyDown(keyCode, event); }
The Question is: How to use event in all fragments i have instantiated in this activity . Thanks
You can use multiple instances of the same fragment class within the same activity, in multiple activities, or even as a child of another fragment.
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
If you want to start a new instance of mFragmentFavorite , you can do so via an Intent . Intent intent = new Intent(this, mFragmentFavorite. class); startActivity(intent); If you want to start aFavorite instead of mFragmentFavorite then you only need to change out their names in the created Intent .
It can't exist independently. We can't create multi-screen UI without using fragment in an activity, After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity.
What you can do is to define a custom method in your fragment class(s). For example:
public void myOnKeyDown(int key_code){ //do whatever you want here }
and call this method whenever a key-down event is raised in your Activity class. For example:
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) { ((PastEventListFragment)fragments.get(0)).myOnKeyDown(keyCode); ((EventListFragment)fragments.get(1)).myOnKeyDown(keyCode); //and so on... } return super.onKeyDown(keyCode, event); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With