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.
Related
I have 4 Activities.
Activity A is where I want multiple types of custom objects to end up for processing.
I used one instance of startActivityForResult to connect from Activity A to Activity B, and from there I use FLAG_ACTIVITY_FORWARD_RESULT when I connect to Activity C. I call this one more time to connect to Activity D.
I can get any of the custom objects back to Activity A easily using the setResult(), my problem is I want to be able to get a Custom Object from Activity D, back to Activity C. Can I use startActivityForResult again from C to D, and when I call setResult on Activity D, will it point back to Activity C or pass down the line due to FLAG_ACTIVITY_FORWARD_RESULT?
Consider to use one host Activity as a navigation/task controller and multiple Fragments with callback results.
If you want to stick to Activities move all navigation logic to the root activity.
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.
I have this situation that I am not sure about the right design/way of doing things.
I have an activity where user would spend most of his time (Call it Activity A). Then the user can go to another activity where it is more graphics intensive (Call it activity B). Activity B would have around 40 Imageviews that have looping drawable animations. The user will be navigating back and fourth between those activities multiple times.
Is the expectation to create Activity B every time the user navigate to it and reinitalize the the 40 views based on the stored Data in my application class (it has coordinates and the type of view created)?
Or is there better way?
Thank you
You can use Following flag with intent to resume same activity without recreating it:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Instead of using activities, you can use fragmentA and fragmentB
When you switch between two fragments, data of them will not be lost.
learn about fragment here
you can use fragments with in the single activity. fragments are easy to swicth between another fragment. so did not lose data of another fragment.
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.
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.