access the variable in activity in another class

后端 未结 4 2044
情话喂你
情话喂你 2020-12-20 01:05

In my application I need a variable from one activity to another activity without using any intent. So I have declared that variable as static and used as FirstActivit

4条回答
  •  有刺的猬
    2020-12-20 02:00

    Try this.

    Step 1: Create a static Bundle object in Application class.( ApplicationClass.java)

    E.g :

         public static Bundle mMyAppsBundle = new Bundle():
    

    Step 2:

    Set key values pair in that bundle from anywhere. like this:

       ApplicationClass.mMyAppsBundle.putString("key","value");
    

    Step 3:

    Now you can get these values from anywhere like this way:

       String str = ApplicationClass.mMyAppsBundle.getString("key");
    

    Please apply null check before using bundle objects for safety points of view.

提交回复
热议问题