Fragment with a ListView calling another Fragment - android

I have an Fragment with a ListView from where i want to call another Fragment. In my BaseAdapter i want to register an onClick and open a new Fragment where i use a ViewPager.
Is here Broadcastreciver/sender the right way to go? or can i just call the new Fragment in the onClick method?
If Broadcastreciver ist the way to go, i would appreciate it if you could help me with the Code. I have never used a Broadcastreciver before. What comes in the onClick method and what needs to be done in my mainActivity.
Thanks

Related

How to move from fragment to activity?

I am stuck in a situation where I have to move from recycler view fragment to an activity but doesn't find a working solution for that.please help
Thanks
Basically, you just need a Context instance to open any Activity:
context.startActivity(new Intent(context, SomeActivity.class)
I assume you are asking about opening new Activity depending on which RecyclerView item was clicked. In that case, you should add View.OnClickListener to the ViewHolder.
The easiest way is to handle click inside ViewHolder. Context instance is accessible from any View using the View.getContext() method. However, I prefer to delegate click back to Fragment/Activity and then open a new screen from there. Check out the sample here: https://github.com/IvanShafran/abbyy-mobile-school-2019/tree/master/RecyclerViewSample

How to update my Fragment List From my Activity?

I would like to change dynamically my List of Fragment which will be sent to an adapter in Parent Activity.
I have tried to send it by a Bundle but the RecyclerView is not refreshed.
I also have tried to implement a method in my Fragment to be called from my Activity.
What's the best way the do that?
Search for observer pattern
https://www.baeldung.com/java-observer-pattern
The idea is making your fragment like an observer for any updates

Use onClick Listener for a button located in one of tab fragment, in Main Activity?

Can I use onClick Listener for a button located in one of tab fragment, in Main Activity?
I think you want to perform some action in your MAIN ACTIVITY when a button is clicked in your fragment. If I am right, then just create a interface callback from your fragment to the activity and use the over-ridden method to perform whatever action you require.
Have a look at this
http://developer.android.com/training/basics/fragments/communicating.html
or this
onAttach callback from fragment to activity
Hope this helps.
Yes, you can think of a Fragment as a stand alone user interface that contains it's own animations and logic. So an onClickListener for a Button in a Fragment is no different than in an Activity.
Though the method using Interface is sufficient, this is the simple way to do it.
You can pass context("getActivity()") of MainActivity while calling
the fragment.
Create a method in MainActivity to change the fragment.
call the MainActivity method to set the Fragment.
((MainActivity) context).setFragment(fragment);
you are done.

Refreshing a fragment view from MainActivity

So I currently have an app that has 4 tabs (fragments). They are fragments A,B,C,D, in that order.
Fragment A is the first view opened (along with B because viewPager loads the view before and after the current view).
When I click a button in Fragment A, it sends Data back to MainActivity and then sends that data out to Fragments B and C.
However, this is where the issue comes into play. Since Fragment B was already called, the View isn't updated once I click the button and send the data over, but Fragment C is because the view wasn't called before.
Is there any way that I can remedy this?
You can do it a few ways right.
Just set the data to the fragment and have it update its views
Have all the fragments like B and C register themselves to recieve data from the MainActivity and when MainActivity gets it's data set you tell all the registered receivers of the new data
Recreate the fragment
Use an event bus and tell all subsribers of the new data and MainActivity, Fragment B would get notified of new data. Fragment C would get its data when created by MainActivity
I think this list is pretty endless tbh
The key here is the fragments need to fetch the data from the actvitiy aswell as be updated by the activity. In which case you need to break your UI update behaviour out of onCreateView and into its own updateUI() function. updateUI(MyData) can then be called from onCreateView and also called in a setMyData() on the fragment. Just make sure you check the isAdded flag in setMyData.
This pretty much says it all:
http://developer.android.com/training/basics/fragments/communicating.html
I used a simple fragment communicator that allows the activity to call the fragment, and the same for a fragment to talk to the activity.
You can change the views with the new data based on calling the method from within the activity. The way I do it is set the fragments in the activity then pass them into the page adapter this way I can call the methods within the fragment and implement the fragmentcommunicator interface on the fragments.
You can honestly even avoid the interface if you want, but if you are going to include the same method in all the fragments to talk to them it is easiest.
If you show code, I can show you a quick example.

Call fragment events from activity

So, I got the event in my fragment to pass to the activity, so how do I make it so the activity in turns, notifies fragment B to do something. I want to fragment B to populate a custom list when fragment A has a list item clicked on. So, it sends the event to the activity, now how do I get the activity to call events in fragment B?
One way to do it would be like this in your activity:
FragmentB fragmentB = (FragmentB)getFragmentManager().findFragmentById(R.id.fragmentBId);
fragmentB.performSomeTask();
This is of course assuming that you have a publicly accessibly method in FragmentB called performSomeTask();
Hope that helps!
The best practice is probably to create interfaces for both fragments and then have the activity implement the interfaces. You want to have good decoupling between fragments so that you can reuse them in other places.

Categories

Resources