I have created a Fragment Activity, It displays Google Map and does other stuff.
Now i have another Activity, I want to use all the functionalities of Fragment Activity into my new Activity.
If i had Fragment, i would have easily used it in my new layout like following.
<fragment
android:id="#+id/calendarFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.activities.fragments.HeaderFragment"
tools:layout="#layout/header_fragment" />
But now i am using FragmentActivity, i cannot use above mentioned code, if i use it it gives me following error message
Caused by: java.lang.ClassCastException
and it obvious, as i passing FragmentActivity into Activity.
Kindly guide me how to reuse FragmentActivity
One simple way would be to use inheritance. Basically make 1 FragmentActivity as the base and then make implementation A (which you already have) that extends the FragmentActivity and then make implementation B which also extends it but does some things differently. It all depends on how much you want to reuse...
Just to make it completely clear. Your current FragmentActivity will be the base. Implementation A will not change very much, while B might change a lot...
Related
Hi I have this one Activity, and I need to send data from it to two different fragments. Is there a way to set this up?
public class MainActivity extends AppCompatActivity implements{ FragmentOne.OnEventListener, fragmentTwo.OnEventListener {
private static String TAG = MainActivity.class.getSimpleName();
So basically I am trying to use one interface and I have all the other methods set up in my two fragments and everything works as intended for fragmentOne. I am just trying to figure out how to make the same listener and interface work for the second interface as well. Is there a way to make it work?
Thanks in advance!
As you wrote, "the same listener and interface", so you don't need to hold two instances of the interface, but just make the mainActivity implements the same one. (if it's not the case post your interface and fragments code).
I assume you are semi-following the android fragment tutorial? Please share the rest of your code.
It should work as long as your activity is correctly implementing both interfaces. Java cannot extend multiple classes but can implement multiple interfaces. However your activity must provide implementation for both of the onEventListeners.
I have this Android project, where from the MainActivity I call some Fragments.
But without including FragmentName.OnFragmentInteractionListeners in the MainActivity I am not able to go to those Fragments.
Public class MainActivity extends AppCompatActivity
implements DirectoryFragment.OnFragmentInteractionListener,
HireDriverFragment.OnFragmentInteractionListener {
What I am worrying about is that I should do this for each Fragment.
If I don't add those Listeners I will get these errors when I try to initiate those Fragments.
must implement OnFragmentInteractionListener
If need more code to provide a better solution I am ready to provide.
What I am worried is that should I do this for every single fragment?
You could create an Interface as Communicating Android Document says, and then you'll have to add the onFragmentInteractionListener on your Fragments.
Also take a look on this Question there are many answers that might help you :)
I cannot decide what approach should I use in the next situtation.
One activity from my app need to have different functionality, here is the leak of multiple inheritence comes into play.
What I need to get ?
There are several parts that my activity has to have.
Navigation drawer. I am using MaterialDrawer library. It requires activity to implement on click callbacks (or use composition instead), but also it use activity as constructor argument,so I think it will be better to put this into separate class inherited from Activity or any base class provided by Android Framework . Thanks to library developer it doesn't require any stuff to be done in on create method ( setContentLayout for instance)
My activity(one of several) will have only Toolbar and FrameLayout for holding fragment . Or it is better to separate toolbar and single fragment ?
This in turn requires some stuff to be done in onCreate : setContentLayout with basic layout , add fragment to the container set activity actionbar .....
Maybe in future I will use another libraries that requires to add something in activity lifecycle methods.
All these points in order to follow Single Responsibility principle have to be separate classes inherited from some base Activity.
For example we will have something like this.
public class SingleFragmentActivity<T extends Fragment> extends AppCompatActivity {
}
public class SingleFragmentToolbarActivity<T extends Fragment> extends AppCompatActivity {
}
public class NavigationDrawerActivity extends AppCompatActivity {
}
....
As you can see each functionality is put into the separate class, but what if I need to have SingleFramgentActivity with NavigationDrawer ?
In this case I have to inherit one of these classes from another, for example
public class NavigationDrawer extends SingleFragmentActivity {
}
If to do so, I have no ability to use navigation drawer separately from SingleFragmentActivity.
What is the best practice to follow in this case, how to build class hierarchy to make it flexible and use Open-Close principle, to make changes in application without any pain. (Use strategy, decorator ..... ?)
I would be grateful everyone for help.
Is there a way to this?
I want to create a fragment, that either extends the support fragment or the default fragment... I think this is not possible, so I ask the question in another way: what's the most beautiful workaround you know/use?
I think this is not possible
Correct.
what's the most beautiful workaround you know/use?
Put the business logic in a separate class (I will call it FragmentHelper). Create one fragment class that extends the native Fragment implementation, and have it delegate work to the FragmentHelper. Create another fragment class that extends the support package's Fragment implementation, and have it delegate work to the FragmentHelper.
My question is apart from the obvious inheritance differences, what are the main differences between Fragment and FragmentActivity? To what scenarios are each class best suited? I'm trying to get an understanding of why both of these classes exist...
A Fragment is a section of an Activity, which has:
its own lifecycle
receives its own input events
can be added or removed while the Activity is running.
A Fragment must always be embedded in an Activity.
Fragments are not part of the API prior to HoneyComb (3.0). If you want to use Fragments in an app targeting a platform version prior to HoneyComb, you need to add the Support Package to your project and use the FragmentActivity to hold your Fragments. The FragmentActivity class has an API for dealing with Fragments, whereas the Activity class, prior to HoneyComb, doesn't.
If your project is targeting HoneyComb or newer only, you should use Activity and not FragmentActivity to hold your Fragments.
Some details:
Use android.app.Fragment with Activity. Use android.support.v4.app.Fragment with FragmentActivity. Don't add the support package Fragment to an Activity as it will cause an Exception to be thrown.
A thing to be careful with: FragmentManager and LoaderManager have separate support versions for FragmentActivity:
If you are using a Fragment in an Activity (HoneyComb and up), call
getFragmentManager() to get android.app.FragmentManager
getLoaderManager() to get android.app.LoaderManager
if you are using a Fragment in a FragmentActivity (pre-HoneyComb), call:
getSupportFragmentManager() to get android.support.v4.app.FragmentManager.
getSupportLoaderManager() to get android.support.v4.app.LoaderManager
so, don't do
//don't do this
myFragmentActivity.getLoaderManager();
//instead do this:
myFragmentActivity.getSupportLoaderManager();
or
//don't do this:
android.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager();
//instead do this:
android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager()
Also useful to know is that while a fragment has to be embedded in an Activity it doesn't have to be part of the Activity layout. It can be used as an invisible worker for the activity, with no UI of its own.
FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.
Well Fragment is good component that copy the basic behaviors of Activity, still not a stand-alone application component like Activity and needs to be attached to Activity in order to work.
Look here for more details
Think of FragmentActivity as a regular Activity class that can support Fragments. Prior to honeycomb, an activity class could not supoprt Fragments directly, so this is needed in activities that use Fragments.
If your target distribution is Honeycomb and beyond you can extend off of Activity instead.
Also a fragment is to be considered as a 'sub-activity'. It cannot exist without an activity. Always think of a fragment as a sub-activity and you should be good. So the activity would be the parent and the fragment(s) the child kind of symbolic relationship.
a FragmentActivity is an ad-hoc activity that contains Fragment.
In these few words I have explain you one of the main important changes that, with android 3.0(HoneyComb), android team has inserted in the android sdk.
With these new kind of concept your pieces of code and layout becomes more flexible and maintainable. If you search on google there are a lot of examples.