I have been making the transition from activities to activities and fragments but I am getting a little confused about how much functionality should go in the activity and how much in the fragment. My initial thought was that the activity simply loaded a fragment (and many examples online work this way). This is fine, but what happens when things get more complicated? Here is a scenario:
You have an activity that loads two fragments, but only one is showing. After clicking a button, the first fragment is hidden and the second shows. Easy enough.
Now, what about if some button on the second fragment needs to call another activity with it's own fragments? Where should the callbacks go for the button listener, in the activity or the fragment? Where should the new activity be launched from? What about if the second fragment needs to call a content provider or a service? Where should that go? What about onActivityResult?
I kind of feel that anything that crosses an activity boundary (starting new activities, getting results etc) should probably go in to the activity, but this is forcing me to bind my activity to my fragments pretty heavily with callbacks, so now I don't know. I am starting to think now that activities should ONLY handle loading and switching fragments to keep them separate, and the inter-process and inter-activity (and even inter-fragment) calls should all happen within fragments.
EDIT: I assume retaining state on an orientation change would probably play into this decision as well!
You can start Activity even Activity for result from Fragment. And also you can receive onActivityResult call in fragment. But remember, to get result from activity in fragment, in Activity you have implement onActivityResult method like this.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Otherwise result won't get to fragment. Parameter requestCode can be used to differ result from various startActivityForResult calls.
I have discovered a very helpful site. It answers my questions and has a lot of information about Fragments and Activities and how they should relate:
http://developer.android.com/training/basics/fragments/communicating.html
Related
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 need a way to share Fragments through different activities, so for example I have MainActivity with a Fragment and when I go to SecondActivity I need that same Fragment loaded, but the Fragment can vary so it would not always be the same one.
I've guessed that I could get the actual Fragments id or tag and pass it on the Intent so I could retrieve it on SecondActivity and use it to load the correct Fragment, but I don't know how.
You can't. You have to create a new instance for the fragment and load again the data in it. If you created the fragment in a good way, it is a really simple task. The reasons why you can't reuse the fragment are the following:
If you could do something like that, what would happend to this fragment's lifecycle? What about going back from current activity to the other? Everything can be messed up.
Every activity has its own FragmentManager and they are never shared between activities, so you can't ask in another's activity for a fragment that doesn't belongs to it.
If you have some doubts on how to pass data using intents between activities to tell the fragment what to load, have a look at this post.
In my android app, I have a fragment where the user can add a picture, either from gallery or from camera. I've created an alert dialog and placed it in a DialogFragment. When the user chooses an option, I call startActivityForResult. My question is, where should ideally this result be handled? (i.e. where should i place onActivityResult?) In the DialogFragment class, the host fragment, or the host activity? Does it matter?
onActivityResult() will be invoked first on the Activity. After that it will be reached out to the Fragments, if you call super.onActivityResult() in your Activity.
This is because of the modular design of Fragments. If a result comes in, the Activity handles it and the unhandled results, if any, can be reached out to the Fragments.
To answer your question: Decide where it makes sense to handle the results in respect of your app design / code. If you handle it in the Fragment and send the results back to the Activity through a callback f.e., you can handle it directly in the Activity.
You must write onActivityResult() in your HostActivity.Java as follows:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//do something
}
Yes, it does matter; It is the activity that receives the result, based on that result you decide what actions to take further, notify your fragments or whatever is that you need.
As I mentioned in the title, I have two questions.
1) I have user details' form in three activities.
Activities Heirarchy:
ActivityOne ---> ActivityTwo--> ActivityDetails1---> ActivityDetails2---->ActivityDetails3---> ResultActivity.
In this hierarchy, details form starts from ActivityDetails1. Submit button is in ActivityDetails3. So when I click submit button, I am submitting all the details that are entered in 3 activities to a database.If the submission is successfull, I am going to other activity(ResultActivity) by intent.
If it fails it stays on the same ActivityDetails3. Whenever submission is sucessfull, I need to finish the 3 of the ActivityDetails activities besides going to other activity. For this I am finishing these 3 activities by accessing contexts by making them static as the marked answer in this link. But this seems inefficient as the static contexts may cause memory leaks. Can some one suggest me an efficient way to do this?
2) After the submission is successful, I have to update even ActivityOne and ActivityTwo apart from going to other activity(ResultActivity) by setting the actionbar items title with one of the details submitted by the user. I am able to make the later activities update the title, but not the previous activities. Can someone please guide me how to refresh the previous activities which are in stack, so that my title of the action item on the actionbar would be changed on previous activities too.
I would be thankful, if someone can help me on my above two questions. Relevant code snippets are appreciated.
Dont type any large paragraphs.Because no one had a patient to read a large paragraph
Type your issues,problemms,what you want in brief,simple,easy to understand.
Refer the below scenarios
Activity A and Activity B.
From A go to B.If B do some operation and finishes then the result is updated in A.
In the above scenario you use following techniques
Put protected void onActivityResult(int requestCode, int resultCode, Intent data)
and handled the updation after exit of B
in A and in B before call to finish() set the result code as you required
How can I pop fragment from backstack without resuming it? I just want to remove from back stack, I don't want to display it.
It's architectural question.
1)Suppose we have 2 controllers (fragments/activities): controller A and controller B.
Both of them connected to one instance of some Model (you may use binding with service, Singleton pattern or init model in Application-heir class and make a getter to it).
When something interesting happens in controller B, he notifies model about it and then model calls controllerA.finish() / controllerA.remove().
Of course, I always try to implement this solution as nicely as possible, but there is a main idea.
2)In another circumstances, I call finish() immediately after startActivity(intent);
3)Also I may write: startActivityForResult(intent, requestCode) and after finishing second activity method onActivityResult(requestCode, responseCode, intent) is called - if requestCode's are equal, I finish the activity.
If you don't use a FragmentTransaction's method public abstract FragmentTransaction addToBackStack (String name) to add it to the back stack. There will not be a need to remove it.