How to pass values from a Class to Activity - Android

前端 未结 6 1262
误落风尘
误落风尘 2021-02-09 08:23

I have a newbie question about Class/Task/Activity. I\'m coming from C so I don\'t know if it\'s a good approach to do what I need.

I\'ve created a class:



        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-09 08:55

    In android 4 option to do this

    • In android you can send data through Intent or Intent followed by Bundle.Like Intent i = new Intent(current_class.this, linked_class.class); i.putextra("Key", value);

    And get the value(suppose string value) in another class like:

     String value = getIntent.getExtra("String key which you used when send value");
    
    • option 2

      class A{

      public static String _utfValue = "";

      void sendValue(){ _utfValue = "some value"; } }

    And fetch this value in your java class like:

    String value = A._utfValue ;
    
    • You can use SharedPreference to save the value and get it from other class.
    • You can use a static method with some return value and fetch the method in your java class through class name.

提交回复
热议问题