Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Tabs and global variables

I am using android TabWidget and I have one main activity where I am managing all the tabs. Each tab has his own activity:

private void setTabs()
{
    addTab("News", NewsActivity.class);
    addTab("Project", ProjectActivity.class);
    addTab("About", AboutActivity.class);
    addTab("Contact", ContactActivity.class);
}

The data for each tab is downloaded from the server in JSON format. My goal is to download all the data on the oppening of the application and then to use it for each tab (activity).

Is there a possibility to share those variables from the main activity to the others where I am managing the tabs? I know about using the Intent class but may be there is another easier solution with the tabs.

Thank you very much.

like image 506
Milos Cuculovic Avatar asked Dec 21 '25 11:12

Milos Cuculovic


1 Answers

Declare static variables in your main activity and store the data in them.

For example, public static integer myInt;

myInt = ...;

Now when you go to the other Activities, just use MainActivity.myInt to access its value and even update it if you want. No need to pass the variable around using intents.

like image 153
Swayam Avatar answered Dec 22 '25 23:12

Swayam