Multiple fragments and activities interaction design - android

Good day, I have three activities with their corresponding fragments A, B and C. Fragment A is a static fragment the others fragments are dynamic.
Activity A is already implementing a listener for fragment A, which is used to load fragment B in landscape orientation or move to new screen in single pane.
What i want is when a certain button is pressed in fragment B which calls up fragment C, I want it to be replaced by fragment C in landscape orientation(fragment A will still be present) or bring up a new screen in single pane mode. I have this simple code in the activity B onCreate method:
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
I am trying to avoid fragment to fragment communication since it is frowned at. So does this mean I have to implement listeners for fragment B in both activity A and B, am guessing that when in landscape orientation activity A would load up fragment C and in portrait, B takes over? Is there a better way?. I thought of only implementing the listener in activity B and passing to activity A when in landscape orientation but I think it would have been finished before it even got to pass due to the above code. Any thoughts?

...so does that mean i implement listener for fragment B in both
Activity A and B, am guessing that when in Landscape Orientation
Activity A would load up Fragment C and in portrait, B takes over or
is there a better way? I thought of only implementing the listener in Activity B and
passing to Activity A when in Landscape Orientation but i think it would have
been finished before it even got to pass due to the above code.
You can't implement the listener just in the B activity because the A activity has to be able to show the fragment C in landscape. If you are worrying about code duplication then you could implement a base activity for which you implement the callback to show fragment C and make your two activities extend from that base class.
As you didn't post any real code on how you manage those fragments, keep in mind that if you are in the portrait orientation(in the B activity showing fragment B) and by clicking(or whatever you do) you replace the fragment with C this will not be carried to activity A if you switch orientation to landscape.

Related

Cannot update fragment's data (fragment in viewpager)

I have 3 activites- A,B, and C. All these 3 activities have a viewpager. And these viewpagers have 1 fragment(this fragment is common to all viewpagers). The launcher activity is A, and pressing a button in A takes me to B, and pressing a button in B takes me to C.
A-> B -> C
Now the problem is when I update any data in C's fragment, it is not updated in A's and B's fragment. But if I update any data in A's fragment and then go to B, B's fragment will have updated data but vice versa is not happening.
Note: I' using a viewpager because I'll be adding more fragments in the future.
I assume the reason this is happening is because when C's fragment is updated, A's and B's fragment is having the old instance of the viewpager (or fragment). So, when I come back to A or B, the old data is being shown.
If this is the problem what is the best solution?
I have tried using viewpager.notifyDataSetChanged(),this works but messes up the views in the fragment.
Can anybody suggest an alternative?Thank you in advance!

How to communicate between Fragment and an non-hosting Activity in Android?

I have an Activity A that is a Main Activity with navigation menu and toolbar that is hosting a Viewpager with fragments. Lets say Fragment B in that viewpager starts intent with Activity C with a button clicked.
Activity C is a Video Player activity which need to trigger a method in Fragment B by using a callback and then Fragment B calls a method in Activity C.
I know Fragment and Activity can communicate using Interfaces, but as you probably understood, Activity C is not hosting Fragment B.
So is it possible to do this kind of interface between the two? Do I need to change my overall design ?
So is it possible to do this kind of interface between the two?
It depends on what exactly you are doing, but as you wrote it, I would give it a solid "no."
In your scenario, Fragment B is not visible when Activity C is displayed. Fragment B will be stopped (i.e. onStop() has been called), and thus you really shouldn't be doing anything with it.
It sounds like you need a slightly different architecture in which the component that needs to be shared between Fragment B and Activity C exists as a separate component that lives independently of Fragment B and Activity C's lifecycles.

Replace a fragment from other activity than the host

I have a MainActivity with a container FrameLayout in which I change multiple Fragments (Fragment A, Fragment B etc).
In one of this fragments let's say Fragment A I have to open another activity (Activity X).
The problem is that from this activity when I press a button I have to change Fragment A with Fragment B (in the background somehow) and after that, slideout Activity X (with translate animation), and slidein Fragment B ,all this without restarting the MainActivity because I have to keep the state.
How can I do this?
Thanks
Android uses loosely coupled components as its main building blocks. As you know, Activities are one of the main Android building blocks. Thus, interacting between activities are very restricted to a few ways.
Passing data via Intents by startActivity(), startActivityForResult() etc. This way is useful whenever you are starting new activities.
Sending broadcast Intents. This could be useful once you want to send a signal to your another app's component.
Utilizing shared Application object.
Java static fields and some other ways.
In your case I would recommend you to use a Dialog Fragment instead of your second activity, if your second activity is just a login activity or something like that.
UPDATE #1:
If you really would like to keep your second activity, so I would personally recommend using local broadcast mechanism.
Also there are another way to get this done. You could start your second activity as startActivityForResult and then whenever user gets back from your second activity to your first one, your first activity can get informed by its onActivityResult method. There you could switch those fragments.

Knowing when a fragment is left from / returned to

Android's Fragment's onResume/onPause methods are tightly coupled with the host Activity's lifecycle as shown here.
What I want to know is how to detect that a fragment is left from / returned to inside the app's navigation flow.
Example:
Say I have MainActivity and fragments A,B and C.
MainActivity adds fragment A, then B and then C.
How do I know fragment B was left (I now see fragment C).
Also, once I press on back, how do I know fragment B was resumed?
Edit:
Clarification: I want to know that from within fragment B (similar to the way an Activity works with onPause and onResume)
Try isDetached() method link here
Respectively there is isAdded()
Indirectly you question concern with Fragment LifeCycle.
And you can trace fragment navigation replace,remove,add using Fragment manager
PopBackStack : Pop the last fragment transition from the manager's fragment back stack. If there is nothing to pop, false is returned.

Android resume from one fragment activity to another and vice versa

I want to create Android app where there are 3 main FragmentActivities, A (main), B, and C, also there are other sub activities. this app can go from activity A to activity B or activity C freely, and vice versa without starting new activity unless it is not started or finished. so how i can achieve this?
----- Edited -----
You can't. At any point in time there is only one Activity that is active. They will be finished and they will be garbage collected.
What you can do is set your launch mode to single task and see if it helps in your situation.
Use Navigation-Drawer or Swipe View..fragments are your solution. So you'll have FragmentActivity and 3 fragment A B C to replace
I will move activities layout to views, and write within one Activity, use hide/show to change to A, B, C.

Categories

Resources