1

I making a static class so I can access it in different activity.. I need to pass 1 data to another activity, but I don't know to make it properly. I tried to use put extra things, but it is for passing data to next activity (not global (maybe)).

this is my class..

static class AuthorizedUserIdClass
{
    private static Guid _authorizedUserId;

    static Guid AuthorizedUserId
    {
        get { return _authorizedUserId; }
        set { _authorizedUserId = value; }
    }
    static AuthorizedUserIdClass()
    {
        AuthorizedUserId = _authorizedUserId;
    }

}

is it right? then, how to use it in activity? and how to call the filled data in another activity?

Thanks in advance :)

Yeremia Danang
  • 139
  • 1
  • 2
  • 12

5 Answers5

3

Put extra pass data one activity to another activity. If you want to access value to whole application label you can use global variable just like

  1. create a class extend of application for storing value application label

     public class GlobalVariable extends Application {
    
       private String message;
    
       public String getMessage() {
          return message;
       }
    
       public void setMessage(String message) {
          this.message = message;
       }
     }
    
  2. Add GlobalVariable to AndroidManifest.xml

    <application
      android:name=".GlobalVariable"     
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name">
      //........ 
    </application>
    
  3. Set Value to global variable like

    GlobalVariable globalVariable = ((GlobalVariable) getApplicationContext());
    globalVariable.setMessage("Hello");
    
  4. Finally get value from Activity like

    GlobalVariable globalVariable = ((GlobalVariable)getApplicationContext());
    Stirng message = globalVariable.getMessage();
    
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
2
public class AuthorizedUserIdClass
{
    private static AuthorizedUserIdClass instance;

    public static AuthorizedUserIdClass getInstance() {
        if (instance == null) {
            instance = new AuthorizedUserIdClass();
        }
        return instance;
    }
    private static Guid _authorizedUserId;

    static Guid AuthorizedUserId
    {
        get { return _authorizedUserId; }
        set { _authorizedUserId = value; }
    }

}

in your activitys.

AuthorizedUserIdClass authorizedUserIdClass = AuthorizedUserIdClass.getInstance();
authorizedUserIdClass.set(value);
authorizedUserIdClass.get();
배준모
  • 591
  • 5
  • 12
1

you can use directly where ever want in your activity or fragment like this

 Guid value = AuthorizedUserIdClass._authorizedUserId;
sasikumar
  • 12,540
  • 3
  • 28
  • 48
1

As far as I understood your case I think you want to save some global data and want to access in different activities.

So what I will suggest is to use the Application class. As it is said that

The Application class in Android is the base class within an Android app that contains all other components such as activities and services. The Application class, or any subclass of the Application class, is instantiated before any other class when the process for your application/package is created.

Read more here and this is the best Question on SO that match your case to save the data.

Please try this and if you find any other problem then let me know

A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
  • and the next paragraph of the docs you quote is: *"Note: There is normally no need to subclass Application. In most situations, static singletons can provide the same functionality in a more modular way."*, https://developer.android.com/reference/android/app/Application.html – pskink Jul 05 '17 at 04:48
  • Yes in many cases we can and we should use the Application class as Singleton – A.s.ALI Jul 05 '17 at 04:55
  • **Note: There is normally no need to subclass Application.** In most situations, static singletons can provide the same functionality in a more modular way – pskink Jul 05 '17 at 04:55
  • who is asking for subclassing – A.s.ALI Jul 05 '17 at 07:36
  • so you dont want to extend `Application` class? – pskink Jul 05 '17 at 07:38
  • so in order `"to save some global data"` please tell me how you can use `Application` class without extending it? – pskink Jul 06 '17 at 07:06
  • All I wanted to say by Application level is that , its life cycle would be as long as the whole application is running and thus the class members, – A.s.ALI Jul 06 '17 at 08:06
1

To pass data between activities you can use the Intent. Just try

intent.putExtra(key, value);

That way you can pass that to another activity.

Besides that you can use preferences. User preferences is another way to save "global" variables and stuff, is also better than a static class.

You can check how to use those in detail in developer.android.com But if you MUST use a static class to call this variables you can just call the class directly. Like, for instance, your class is called Util, then you can just call

Util.publicmethod();
int c = Util.publicIntVariable
EduardoMaia
  • 591
  • 1
  • 4
  • 17
  • intent filled with var intent=new intent(this, typeof("sampleActivity")); soo, only in next activity we can get the value from extra? – Yeremia Danang Jul 05 '17 at 06:29
  • 1
    Its like that. `Intent intent = new intent(this, sampleActivity.class) intent.putExtra("someValue", "value") startActivity(intent) Then in the next activity you can just String v = getIntent().getExtras().getString("someValue")` v will be "value" – EduardoMaia Jul 05 '17 at 13:46