0

How come when I need to access a variable or set one in another Activity it resets it when I go

new MainActivity()

for example, I get an input in SecondActivity and want to set a Variable in MainActivity

String input1 = editText.getText().toString();
int input=Integer.parseInt(input1);
MainActivity ma = new MainActivity();
ma.setTableNumber(input); 

but when I go to get it in ThirdActivity it is 0 again

int a = Interget.toString(new MainActivity().getTableNumber());

is there a way to avoid having to go new MainActivity() every time?

this is how I go from MainActivity to SecondActivity btw

Intent loadTableNumberScreen = new Intent(MainActivity.this, TableNumber.class); startActivity(loadTableNumberScreen);

If that helps at all

ramana vv
  • 1,141
  • 11
  • 21
Biomechanic
  • 85
  • 1
  • 8
  • Are you looking to transfer data from MainActivity to another? – letsCode Sep 13 '18 at 13:17
  • 6
    You should never be directly instantiating activities. Android instantiates them for you. Attempting to create your own with new will at best not work, at worst cause you to crash – Gabe Sechan Sep 13 '18 at 13:17
  • @GabeSechan how do I access methods in them then? – Biomechanic Sep 13 '18 at 13:18
  • @DroiDev yes basically, one of which is an array of objects – Biomechanic Sep 13 '18 at 13:19
  • You need classes to access methods.... You shouldn't access methods inside of an activity. This is where OOP comes into play. – letsCode Sep 13 '18 at 13:20
  • 2
    @Biomechanic You don't access those methods directly. If you wish to exchange simple data between Activities, you can add it as an `EXTRA` in the `Intent` you use to start your second `Activity` and read the value there. See https://stackoverflow.com/questions/5740324/what-are-the-ipc-mechanisms-available-in-the-android-os and https://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents – PPartisan Sep 13 '18 at 13:21
  • @GabeSechan I think you comment is the most correct answer to this question, you should post it. – m0skit0 Sep 13 '18 at 13:23
  • @Biomechanic If you what exchange data between two activities you can use Bundle. – Mirza Adil Sep 13 '18 at 19:25

1 Answers1

-2

The approach you are trying is redundant and very bad practice.When you call new Activity() you simply create a new reference.You didn't stored the reference that you made while calling new Activity you should store that reference in a variable. But instead of doing that you can use a singleton class or make some static variables in your activity for accessing them globally within your app.

Zubair Soomro
  • 165
  • 11
  • This is bad advice, for a few reasons. First, `Activity`s should never be created via their constructor - instead, call `startActivity()` on a `Context`. Second, static variables are the worst way to share information between Activities, exactly because they are global (modifiable from anywhere, so it is more difficult to track their state), and because the Activity may be destroyed by the OS (see https://stackoverflow.com/questions/12189476/public-static-variables-and-android-activity-life-cycle-management). – PPartisan Sep 13 '18 at 14:41
  • I know that too its a bad way . i was just answering his question that what happens when he calls new Activity() that's all i didn't encouraged him to do that. First understand what i said. – Zubair Soomro Sep 14 '18 at 07:36