Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I'm developing an android based restaurant menu. My problem is the setting of tablenumber. How can I set a table number then pass it around the classes?

What I have tried:

I've tried global classes, but I can't make it work.
Posted
Updated 30-May-16 11:35am
Comments
Richard MacCutchan 30-May-16 16:17pm    
Your question is far from clear. Passing information between objects of different classes is a basic part of OOP.

How about?
public static int tableNumber;

This would be accessible. Setting it in the main activity may help you in what you need. But since this is Android, there is a much easier and cleaner way of passing the data to another activity, like this,
Java
// Create the intent
Intent activity = new Intent(this, SecondActivity.class);

// Set the data
activity.putExtra("tableNumber", 1234);

// Start the activity
startActivity(activity);

Later, on the second activity you can get the value from the intent that started this activity.
Java
Intent previousActivity = getIntent();

// Get the value
int tableNumber = previousActivity.getIntExtra("tableNumber", 0);

In my opinion, this is much efficient way of sharing the values and data from one activity to another. Although static variables may work, but they are not recommended ways of passing the data from one activity to another.

I was going to write an article about this, but since it is not yet available, you may want to read this instead. android - How to use putExtra() and getExtra() for string data - Stack Overflow[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-May-16 17:28pm    
I would strongly warn against using anything global. This is not really needed but causes a lot of problems. In worst case, a fully-fledged singleton pattern can be used; at least it would make access controllable.
Please see Solution 2.
—SA
Afzaal Ahmad Zeeshan 30-May-16 18:17pm    
That is exactly why I wanted him to stop there and to use a much Android-oriented method of passing the data with the intents; if the purpose is to share the data only. :-)
Sentell 1-Jun-16 5:02am    
I think this is a good method, just 1 question. If I start the first activity then go to second activity (I'm already holding the value now), then I go back, would that lose the value?
Afzaal Ahmad Zeeshan 1-Jun-16 5:08am    
The value would be removed only if the activity is cleared from the memory. Chances are that when you get back to the activity, previous activity has the same values and state as it had before you navigated away.
Sentell 1-Jun-16 5:57am    
No I don't plan on changing it anyway, what i'm trying to do is, set the value at the first activity then pass it around.
Using anything global is a really bad idea.

First thing to consider for having a reference to one single object shared in many places of your application is some local object with reference passed along to all types/methods where it should be used. This is quite possible, if you plan it thoroughly.

I understand that this could be a bit too much of painstaking work. Then you can consider the second option: singleton pattern.
Please see:
Singleton pattern — Wikipedia, the free encyclopedia[^],
Simply Singleton | JavaWorld[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900