The use of this tutorial is very clear to me however I am using the Actionbar.tablistner interface. How can I pass variables(strings) from fragment to fragment using this interface?
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
}
Create an interface with a method which accepts any variable (e.g. String). Something like this:
public interface TabClickedListener {
public void passParam(String var);
}
Implement this interface in your Activity. From the Actionbar.tablistener onTabSelected() method call the above interface method (on the activity instance you have) passing whatever value you would like. Once you receive this value in your Activity you can pass this to a different Fragment.
Related
I seem to be stuck with a problem with an object communicating with my activity class. The object is a view object with an onClick method that when called I would like it to notify my activity class so that it can perform said action. Below is some example code of my situation (assume all conventional setup operations have already been made):
public class MainActivity extends AppCompatActivity{
//...other global methods and objects
//Does not have access to instantiated Entry object(s)
public void entryObjectWasClicked(){
//perform said action
}
}
public class Entry extends View implements View.OnClickListener{
//...other global methods and objects
//Does not have access to the MainActivity object
#Override
public void onClick(View v){
//send a message to the MainActivity to
//somehow call the entryObjectWasClicked() method
}
}
The only way (off the top of my head) that I could think about dealing with this problem is by creating a static method in MainActivity and then calling it from an anonymous MainActivity object in the onClick method of Entry. The problem with the static method approach is that any subsequent method/object/primitive usages in the static method force those methods/objects/primitives to be static. This defeats the purpose of then being able to have two different instances of the MainActivity object.
After some looking I came across using Broadcast messages, specifically using the LocalBroadcastManager to send an intent to the activity. This code example works for my model, but I want to know: is this the best way for me to go about sending messages to my MainActivity from my Entry object?
If there is a more effective way of doing all this, what would it be?
You're overcomplicating things. Don't override onClick for this. Instead, have your activity call setOnClickHandler on your view, which sets a callback that's called when the view is clicked. Then use the default implementation.
Since you extend view, i guess you want to use it inside a layout. That means you may want to create a Listener for that. Example:
public class Entry extends View implements View.OnClickListener{
private OnClickListener listener;
public void setListener(OnClickListener listener) {
this.listener = listener;
}
#Override
public void onClick(){
if (this.listener != null) this.listener.onClick(this);
}
}
How you can inflate your layout in your Activity and access your custom view.
public class MainActivity extends AppCompatActivity{
public void onCreate( ...) {
Entry entry = findViewById(R.id.entry);
entry.setListener(new OnClickListener(...));
}
}
i have a created a fragment and can't find what is the use of Interface class in this Fragment...i google it but can't find the right documentation?
Thank you for your concern!
public class SongListFragment extends Fragment {
public SongListFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
songIds = getArguments().getIntArray(SONG_IDS);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
//what's the use?
public interface OnFragmentInteractionListener {
public void onSongSelected(int songId);
}
}
OnFragmentInteractionListener could be use to communicate between fragments
To allow a Fragment to communicate up to its Activity, you can define
an interface in the Fragment class and implement it within the
Activity. The Fragment captures the interface implementation during
its onAttach() lifecycle method and can then call the Interface
methods in order to communicate with the Activity.
Find another SO example here
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
You might have a brief idea over here Feel free to ask if any confusion rises! :)
So in your particular case your Activity must implement that interface OnFragmentInteractionListener otherwise the fragments which are attached in the Activity cannot communicate with each other. Your activity should look like
public class YourActivity extends Activity implements OnFragmentInteractionListener
Then in your Activity you implement the method onSongSelected(int songId)
You might get help from here. Hope this helps!
#Tahmid Rahman explains what an interface is in there answer.
In this specific case the interface should be implemented in the Activity that your fragment is attaching to. That will allow the fragment to call onSongSelected() on the activity. Then the activity can in turn properly handle the users requested action.
Without the interface, there would not be a well defined way for the fragment to tell its parent activity that the user had clicked on a song.
Interface is used to communicate between fragment and activity or between multiple fragments when an event occurs.
See the Documentation
in your case:
public static class MainActivity extends Activity
implements SongListFragment.OnFragmentInteractionListener{
...
public void onSongSelected(int songId) { //this method must be implemented
// The user selected the song from the list in SongListFragment
// Do something here to display that song..in your activity
}
}
you can implement the interface in your activity and write the onSongSelected method with the song id passed as parameter.
So basically it is used to pass selected song list information to other activity or fragment when a selection occurs
There is an asynckTask and 2 methods,which are being called by 2 activities.
i Want to keep the AsyncTask class and the methods inside myApplication class
http://developer.android.com/reference/android/app/Application.html
( which was needed anyway,had some states of app to be maintained).
One other way is to have those methods in each activity and the asyncTask as independent class.
what is the best way?
How about having a base activity class for that?
Something like:
public class BaseActivity extends Activity {
protected void myMethod() {
// do what ever
}
}
Then just extend this BaseActivity to have that method in your activities.
I'm going to create a Fragment that have an Activity to managing that fragment; Like this:
public class Form extends Fragment {
// TODO some code ...
public class Dialog extends FragmentActivity {
// TODO some code ..
}
}
but it show me error, So I crated it with a static inner Activity but an static inner class don't get me that accesses.
There's no way to make this work. Android requires Activities to have a public no-arg constructor. Non-static inner classes can't be created without an instance of the outer class, and the Android framework doesn't have (and very much shouldn't have) a way to instantiate a Fragment for the purposes of instantiating an Activity.
I want to use different AsyncTaskLoaders (different in their return type) in my Activity, what's the best way to implement the callback methods?
This won't work:
public class MyActivity extends Activity implements
LoaderManager.LoaderCallbacks<MyPojo>,
LoaderManager.LoaderCallbacks<MyOtherPojo>
Eclipse says
The interface LoaderCallbacks cannot be implemented more than once with different arguments
So what do I do? My idea is to make the Activity
implements LoaderManager.LoaderCallbacks<Object>
then check in the callback methods what type of object it is but that doesn't seem too elegant. Is there a better way?
What about creating an inner class for each callback?
public class MyClass extends Activity {
private class Callback1 implements LoaderManager.LoaderCallbacks<MyPojo> {
...
}
private class Callback2 implements LoaderManager.LoaderCallbacks<MyOtherPojo> {
...
}
}