Call Activity Method From Fragment

后端 未结 5 939
南笙
南笙 2021-01-14 10:12

I\'m dealing with fragments.
I have an Activity and different fragments.
Each fragment need the access to a Class(call i

相关标签:
5条回答
  • 2021-01-14 10:34

    This is a little bit more of a Java question and android.

    If you looking at accessing the database, look at creating a database singleton.

    So something like:

    public class Database {
    
        // This starts off null
        private static Database mInstance;
    
        /**
         * Singleton method, will return the same object each time.
         */
        public static final Database getInstance() {
            // First time this method is called by Database.getInstance() from anywhere
            // in your App. It will create this Object once.
            if(mInstance == null) mInstance = new Database();
            // Returns the created object from a statically assigned field so its never
            // destroyed until you do it manually.
            return mInstance;
        }
    
        //Private constructor to stop you from creating this object by accident
        private Database(){
          //Init db object
        }
    
    }
    

    So then from your fragments and activities you can then place the following field in your class's (Better use use a base activity and fragment to save you repeating code).

    public abstract class BaseFragment extends Fragment {
    
        protected final Database mDatabase = Database.getInstance();
    
    }
    

    Then your concrete fragments can extend your BaseFragment e.g. SearchListFragment extends BaseFragment

    Hope this helps.

    Worth reading about singletons and database

    Regards, Chris

    0 讨论(0)
  • 2021-01-14 10:34

    you have to Additional cast need to be done:

    Activity activity123 = getActivity();
    
    if(activity123 instanceof ParentActivity) {
        ((ParentActivity) activity123).someMethodInParentActivity();
    }
    
    0 讨论(0)
  • 2021-01-14 10:42

    From the fragment call your activity's method

    ((MyActivity )  getActivity()).getClassX() ;
    
    0 讨论(0)
  • 2021-01-14 10:48

    Define an interface called Callbacks (or something else if you want). In it, have a public method called getClassX(). Then make your Activity implement the Callbacks interface.

    In your Fragments, in onAttach, store a reference to a Callbacks object (i.e. your activity via something like:

    if(activity instanceof Callbacks)
        mCallbacks = (Callbacks)activity;
    

    This will guarantee that the Fragments are able to call the function. (in case you want to reuse the fragments later in another app)

    Then in your Activity, in onCreate(), create an instance of ClassX. In your getClassX() method, just return a reference to it.

    When you want a reference to it from your Fragments, call mCallbacks.getClassX() and you should be sorted.

    0 讨论(0)
  • 2021-01-14 10:54

    You can use a static object in your activity, and use it from the fragment, or call the getActivity() method in your fragment to access the whole activity objects/methods

    0 讨论(0)
提交回复
热议问题