Communicative Between Fragments on Android

后端 未结 5 1549
日久生厌
日久生厌 2021-01-21 19:21

I am attempting to build an Android application that has two tabs, one for a textField/button and TreeMenu (where each element has a checkbox associated with it) and another for

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 19:46

    As John said, when you want to access data in several activities, just create a class that extends Application:

    import android.app.Application;
    
    /**
     * Application class used to share data between activities.
     * @author Longeanie Christophe
     *
     */
    
    public class MyApplication extends Application {
    
    
        //Put your class members here
    
        @Override
        public void onCreate() {
            //initialize what's needed here
        }
    
        //Getters and setters...
    }
    

    In all your activities, you can access this class and its data through this instruction:

    MyApplication myApp = (MyApplication) getApplication();
    

提交回复
热议问题