Difference between fragment and intent - android

We use intents to switch between two activities and also fragments are for the same purpose. So why can't we use intents always instead of fragments?

Intent, you can think of intention to do some work. It can be either go from one activity to another activity, send email, open some links and so on.
Fragment is just like part of those activities.
To make it simple you can think of activities as full website page whereas fragment as a part of that website page. So, activitycan contain any number of fragments.

I think :
1.In the changing fragment you just change part of activity and not whole of them , but by intent you change whole of activity to other.
2.by intent you can communicate between android components such as activities, content providers, broadcast receivers and services, but by fragment you cant and in the otherwise fragment is child of activity.

Related

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.

How to share fragments through activities?

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.

An Activity with two different Activities as Parent

I have an Activity DetailsActivity that is accessible from two different Activities: MapActivity and ListActivity. How should I implement Up Navigation to select proper Activity as Parent?
You can set a flag while going into the details activity from any one of the activities. Then onBackPressed you can fire that particular intent depending on the flag you first sent in.
I had the same problem and in my case rearanging the code worked perfectly. If you have the same logic in two classes, rather then inheritance, try to create another class, which will do the whole work for us.
Simple add a new atribute to Details Activity and (List Activity or Map Activity).

How can I "start a Fragment with an Intent"?

first of, I know that it is not possible to start a fragment via an Intent like you do with activities. However, in my application I would like to have the functionality that I can return from the activity to a fragment on a button click.
How do I do that? Any suggestions?
You can't "return from an Activity to a Fragment", simply because Fragments need to be put somewhere. Which means that hey need an Activity as a container. Putting aside some dirty overlay tricks.
So in the usual cases, you need an Activity to hold your Fragments. And since you have an Activity, you can have an intent-filter to handle your intent.
Activity hosts fragment, I think you should return to an Activity and select the correct Fragment. But there's always problem if you wanna select a Fragment freely, because you might have had a Fragment-Stack. If you don't have, ignore what I wrote below.
You have maybe three chance to reach your points. If you've called addBackStack, by default the Android use a backstack to control your Fragments, you have no chance to select your Fragments freely. so....
Chance 1 maybe worse case: Do not use addBackStack, and always use replace to finish your fragment's transaction.
Chance 2: Use FragmentTabHost. Then you can free select your fragments you've created, but be care of their life-cycles.
Chance 3: Use ViewPager, u know it ! :)

Accessing sibling tabs in an Android application?

I have an Android application using a TabHost to create several tabs with different activities in each tab.
When someone performs a certain action in the activity associated with tab A, I want to programatically switch to tab B, and call a function on tab B's activity to provide it with some data generated in tab A.
Is such a thing possible? I don't know how to access either the TabHost object or the B activity from within the A activity. I tried passing these objects in to the A activity by adding them as "extras" to the Intent, but this doesn't work, probably because the classes are not serializable. If I could access the TabActivity itself, that would probably be the best solution.
Thanks,
Frank
Implement a BroadcastReceiver in the TabActivity and another in Activity B.
Register both in the manifest with custom intent filter 'actions'
Get Activity A to send a broadcast with an Intent including extras that the TabActivity will receive.
Have the TabActivity switch tabs then send a broadcast with an Intent with extras that Activity B will receive/process.
To expand on this, think of it this way...
Under 'normal' circumstances an Activity is a stand-alone / self-contained entity. If it needs to have some action performed by another Activity, it shouldn't need to know anything about that other Activity, just a way to 'call' it, i.e., use an Intent set with the relevant action/category and extra data.
In the case of an Activity which is embedded as tab content, this mostly still applies. The Activity itself doesn't 'know' that it has been embedded as tab-content (nor should it). As such it knows nothing about the TabHost or Tabactivity (when using one). It also doesn't/shouldn't know specifically that there are other tabs with other Activities...
It is quite legitimate, however, for the TabActivity to know about the tab contents (it created them after all) and to be able to mediate between them. If you simply have Activity 'X' broadcast that a particular action has been performed and allow the TabActivity to receive and act on the Intent type/data then the various siblings don't need to know specifically how each other works.
That would be my approach anyway. :-)
You could stash the data in an object at the Application level. Then when activity B starts it could check for the data.

Categories

Resources