Android: How to constantly update / transfer values ​​from one activity to another?

I pretty much do a custom countdown timer for one action, and I am having difficulty constantly updating the value when switching actions. I can trick a value only once, every time I change activity.

Example:

Activity A → B (default 0 on screen, user updates value to 10)

B → A → (10 on the screen, the value of user updates to 10)

B → A → (35 on the screen, the value of user updates to 9000), etc.

However, as soon as the user sets the time, he must constantly update it and count it.

I can update the value behind the screen with Runnableand print the values ​​on the console, but I don’t know why it will not constantly transfer the value back to the previous activity A (so that it is up to the date), and then A transfer it back to B when the user will return to B.

I should note that when I try ONLY to update additional functions in run()and not in updateTimerButton(), nothing on the screen changes, there are default values, but the values ​​are updated behind the scenes (console).

Here is what I have:

public class CustomTimer extends Activity {

    private final Handler handler = new Handler();

    private Intent intent;
    private Button timerButton;
    private EditText hourValue, minuteValue;

    private long startTime = 0L;
    private long timeInMilliseconds = 0L;
    private long timeSwapBuff = 0L;
    private long updatedTime = 0L;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_timer);

        intent = getIntent();
        hourValue = (EditText) findViewById(R.id.hour_value);
        minuteValue = (EditText) findViewById(R.id.minute_value);
        timerButton = (Button) findViewById(R.id.start_timer_button);

        //avoid grabbing values from hour and minute if they were not passed for some reason
        if(intent.getStringExtra("hourValue") != null)
            hourValue.setText(getIntent().getStringExtra("hourValue"));
        if(intent.getStringExtra("minuteValue") != null)
            minuteValue.setText(getIntent().getStringExtra("minuteValue"));

        setUpHourValue();
        setUpMinuteValue();
        setUpTimerButton();
    }

    private void setUpHourValue(){

        hourValue.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                hourValue.setText(v.getText().toString());
                return false;
            }
        });
    }

    private void setUpMinuteValue(){

        minuteValue.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                minuteValue.setText(v.getText().toString());
                return false;
            }
        });
    }

    private void setUpTimerButton(){

        timerButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                String hourTime = hourValue.getText().toString();
                String minuteTime = minuteValue.getText().toString();

                //only start the timer if it is not zero
                if (!hourTime.equals("00") || !minuteTime.equals("00")) {
                    System.out.println("starting runnable");
                    startTime = SystemClock.uptimeMillis();
                    handler.postDelayed(updateTimerThread, 0);
                }
                else{
                    System.out.println("time is default\t" + hourTime + "  : " + minuteTime + "************");
                }

                //send the time values back to main
                //this does not constantly update the values, it only updates it once
                intent.putExtra("hourValue", hourTime);
                intent.putExtra("minuteValue", minuteTime);
                setResult(RESULT_OK, intent);
                finish();//go back to the previous page
            }
        });
    }

    private Intent getOuterClassIntent(){
        return intent;
    }

    private Runnable updateTimerThread = new Runnable() {

        //update the time on the screen and constantly pass the values back to main
        public void run() {

            String hourTime = hourValue.getText().toString();
            String minuteTime = minuteValue.getText().toString();

            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            int hour = mins / 60;
            secs = secs % 60;

            //update the time on screen
            hourValue.setText(Integer.toString(Integer.parseInt(hourTime) - hour));
            minuteValue.setText(Integer.toString(Integer.parseInt(minuteTime) - secs));

            System.out.println("updating time in runnable");
            System.out.println(hourValue.getText().toString()+ " : " + minuteValue.getText().toString());

            //pass the values back to main, but this does NOTHING if I comment out the previous putExtras (above)
            getOuterClassIntent().putExtra("hourValue", hourTime);
            getOuterClassIntent().putExtra("minuteValue", minuteTime);
            setResult(RESULT_OK, getOuterClassIntent());

            handler.postDelayed(this, 0);
        }
    };
}
+4
source share
2 answers

, . , , , ( Binder), . .

+3

, event bus activity.

EventBus.getDefault().register(your activity);

, A B

EventBus.getDefault().post(your value to post);

OnEvent(your data type),

refrence, , this

0

Source: https://habr.com/ru/post/1629853/


All Articles