Search code examples
androidandroid-intentintentfilterandroid-intentservice

Using intents in android is causing problems between activities


I have run into a problem. I have an animation that is going in my first activity, and a countdown timer in my second activity. When I set the countdown timer the value is passed back into my first activity which begins a new countdown activity, but the animation stops. How can I pass the value of the countdown timer to my MainActivity while still Maintaining the animation in my main activity.

Here is my code for the intent in my SecondActivity that is being passed to my MainActivity. Im not sure what code to show because I am not sure what the cause of the problem is.

buttonStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //put your logic here to set the time with hourPicked and minPicked variables
            timer = new CounterClass((hours * 60 * 1000) + (minutes * 1000), 1000);
            String hms = String.format(("%02d:%02d"), hours, minutes);
            textViewTime.setText(hms);
            Intent intent = new Intent(SleepTimer.this, MainActivity.class);
            String TimeValue = (hms);
            intent.putExtra("TimeValue", TimeValue);
            startActivity(intent);
            timer.start();
        }
    });

Solution

  • You have to declare String hms globally and onclick of button finish the SleeoTimer activity.and access global variable hms at MainActivity in SleepTimer Activity create field as public static String hms; and in MainActivity use it as SleepTimer.hms

    buttonStart.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            //put your logic here to set the time with hourPicked and minPicked variables
            timer = new CounterClass((hours * 60 * 1000) + (minutes * 1000), 1000);
             hms = String.format(("%02d:%02d"), hours, minutes);
            textViewTime.setText(hms);
           SleepTimer.this.finish();
        }
    });