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 {
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.
First of all i will apologize for the bad question formulation, i don't know how to ask is on a more clear way, so suggestions are welcome!
I'm new with android and struggled a lot with aSyncTask for getting some XML from the web. Downloading the XML and pass it to the class who invoked the aSyncTask works fine now.
The problem i have is that i only can listen to the event which is fired in the aSyncTask > onPostExecute method when i put "implements Interface" on the class who invokes the aSyncTask class. I'm interessed to an way where i dont need to implement the interface. I think this is posible becouse the default objects such as buttons and so on can be listened to witouth implementing something.
Maybe i think completely wrong about this all?
The way i set up my own event handler/listener is:
Creating an listener
public interface XmlUpdateListener {
void onXmlUpdate(String result);
}
Invoke the listerner in the aSyncTask class
public XmlManager(XmlUpdateListener delegate) {
this.delegate = delegate;
}
#Override
protected void onPostExecute(String result) {
delegate.onXmlUpdate(result);
}
Listen to the event in the class who invokes the aSyncTask class
public class XmlDataLayer implements XmlUpdateListener
#Override
public void onXmlUpdate(String result)
{
this.XML = result;
}
I searched for a lot of videos on youtube and a lot of articles on the internet. There are many many ways explained but i don't realy understand one completely. Here on stackoverflow i found a lot of articles but the most of them has no 100% clear answere for me. Maybe someone has a lot of patience to make me this whole stuff clear for ever by give me some sample code and explain me what happened. I'm a person i need to know how it works and why it works like that, then i understand it else it is a big mess in my head and can't still understand.
---------- Edit: Added some extra info about my problem ----------
My problem is: from UI i call an dataclass, this gets starts the aSyncTask and get its response over the upstanding mechanism. From there i need to trow an event that can be used on several places. I will catch this event for updating the UI and updating the internal database. The problem is that my UI class extends the AppCompatActivity and i cant implement the interface there to catch the event. That's the reason why i need to catch an event without implementing something. I think something like new XmlManager(..).exe(); looks good but cant get it done without "implements XmlUpdateListener"
I'm trying to create a class diagram for an android project.
I want my classes represent the activities, services and interfaces that I will implement.
There are several questions about it on the web, but I couldn't find a definitive answer.
I know that there aren't specific rules for Android and UML, but I have some doubts.
How can I represent the relationship between an Activity and a AsyncTask ?
How can I indicate that an Activity has an intent to another Activity ?
Maybe if someone has an example, will really help.
Maybe something like this?
MyBackgroundTask IS A AsyncTask
MyActivity IS A Activity
MyActivity HAS ONE MyBackgroundTask
You can use a dependency to show that there is an relationship between the AnActivity class and the AnotherActivity class:
In this case, it means that the AnActivity class requires the AnotherActivity class for its specification or implementation. Also, use notes to make it clear.
Update:
Another example:
MainActivity IS A FragmentActivity
MainActivity HAS SOME fragments. The fragments are of type String.
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 :)
Is declaring a class that extends Activity inside another Activity class possible? If it is, how would I register that class in the manifest? Also, is that something that can be reasonably done or is it a bad idea?
I was thinking of something like
class ListClass extends ListActivity{
...
ArrayList items;
class ItemClass extends Activity{
...
Item item;
#Override
onCreate(){
Integer pos = getIntent().getExtras().getInt("pos");
item = items.get(pos);
}
}
#Override
onItemClick(int position){
startActivity(new Intent(this, ItemClass.class).putExtra("pos", position));
}
}
Note the syntax isn't 100% correct obviously, mostly pseudocode.
Yes, it does work -- it is just another class -- you just need to declare your activity using inner class notation in AndroidManifest.xml:
<activity android:name=".ListClass$ItemClass"/>
Seems to work fine for me, but perhaps when this question was asked, it wasn't supported in older versions of Android?
Not sure WHY you'd want to do this, but you can.
No, that's not possible. After all, the Android operating system will need to instantiate the Activity if it is started at any point (say, if you start it through an intent), and it's impossible to instantiate an ItemClass without a parent ListClass.
Remember that each Activity is completely independent and can be started at any point through an intent.
I'd also be curious why you'd want to do this.
However, I don't see any reason why it wouldn't work. Couldn't you reference it in the AndroidManifest as you normally would as long as both classes are public? i.e. com.falmarri.ListClass.ItemClass?
Edit: Nevermind, this doesn't work as EboMike pointed out.