0

is it possible to get all activities in the application? i have a global integer variable that should be in the ActionBar of every activity. i thought something like this:

for (Layout/Activity l in (all activites)) {
    l.setTitle(variable);
}

i already tried it with R.layout but this didnt work for me.

How can i do this or is there a better way to display my variable in all activity labels? later i want to call this code from my set method for the global variable.

vtni
  • 950
  • 3
  • 14
  • 43
  • Only one activity can be active (displayed) at the same time. What you think you want to do has no sense. Update action bar from each activity or create a parent activity class which will update action bar. Extend all your activities from that class – Alexander Zhak Aug 30 '14 at 21:42
  • my variable is changing by user interaction. if he opens another activity the actionbar should be up to date. how can i get the active activity? – vtni Aug 30 '14 at 21:44
  • you could also subclass Application and store your variable there and access if from any activity. See http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables for more info – Alexander Zhak Aug 30 '14 at 21:46
  • Use SQLite to save changes... so when new activity gets open it should read first from the database that what changes are made. – Jamil Aug 30 '14 at 21:54

2 Answers2

0

yes it's possible with singleton. This is how to use singleton:

This is Singleton class:

    public class Singleton {
        private static Singleton mInstance = null;
        private String mTitle;

        public void setmTitle(String mtitle){
        this.mTitle=mtitle
        }

        public String getmTitle(){
        return mTitle;
        }

        public static FilterArrayList getInstance(){
            if(mInstance == null)
            {
                mInstance = new FilterArrayList();
            }
            return mInstance;
        }
    }

This is the first activity:

public class FirstActivity extends Activity {

    @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       Singleton.mInstance.setmTitle("This is Singleton");

   }
}

and in second activity:

public class SecondActivity extends Activity {

   String Title;

    @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


       Title=Singleton.mInstance.getmTitle();

   }
}
MilanNz
  • 1,323
  • 3
  • 12
  • 29
0

There is only one activity running at a time, so you can’t get this kind of references.

Said that, I think the way to go it’s create an int static variable in some class, and called it from your activities.

//SomeClass

public static int xValue = 0;

//ActivityOne || ActivityTwo || ActivityThree ...

String text = String.valueOf(SomeClass.xValue);
SomeClass.xValue = 1;

Because it’s a public static variable, you don’t need to instantiate any object to get/set its value, and it will be accesible from any class. Furthermore, this value will be reachable as long as its class is in the memory, and destroy just when class gets unloaded.

Community
  • 1
  • 1
Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71