-2

I want change a value from another activity like this

Main_activity.java

...
long notify_interval = 1000; //ms
...

But, In another activity, called system.java, I have this code

btn_normal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Main_activity.notify_interval = 1000; // HERE <<--
            Toast.makeText(getApplicationContext(), "Interval changed!", Toast.LENGTH_SHORT).show();
        }
    });

But isn't working, someone tells me that this way never will work. So, what should I do?

--------- UPDATE ---------

Hello, with this trick, I'm able to change long value between activities

 public static long notify_interval = 1000;

But, I'm using AlarmManager, when I close the app, instead of using the custom notify_interval that I set, the system is using the default value (1000). So, what should I do to fix it?

Giuseppe
  • 33
  • 1
  • 6

2 Answers2

1

If u need to do it in real-time, make interface and make activity implement it then pass it where u are updating value and call method that updates value in activity

else u need to add extra to intent

another way is to use RxJava or EventBus.

svkaka
  • 3,942
  • 2
  • 31
  • 55
1

You can use static variable to do that.

public static long notify_interval = 1000;

On second activity use

btn_normal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Main_activity.notify_interval = 1000; // HERE <<--
            Toast.makeText(getApplicationContext(), "Interval changed!", Toast.LENGTH_SHORT).show();
        }
    });

Its worked fine.

Saheb Roy
  • 161
  • 4