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 :)
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 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...
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.
Here is the post I need clarified (I am unable to comment on this post because I am a new user, and I tried asking a question below it that just got deleted):
How to implement OnFragmentInteractionListener
I am getting the exact same error as this user, under the exact same scenario, and on his post, he says it is working, however he never explicitly explains what exactly he did. Can anyone shed some light on how this was solved??
Where exactly does the
implements OnFragmentInteractionListener
go?
because currently the activity I am trying to call the other fragments in is as follows:
public class MyActivity extends Activity implements NavigationDrawerFragment.NaviationDrawerCallbacks{
I have no clue where to slot in the missing implements.
Any and all clarification helps!
My exact problem is defined in that above post on StackOverflow, let me know if I can do anything else to clarify this question!
Thanks!
A single class can implement more than one interface:
public class MyActivity extends Activity implements OnFragmentInteractionListener, NavigationDrawerFragment.NaviationDrawerCallbacks {