简体   繁体   English

我如何将计时器从一个片段传递到另一个片段?

[英]How would i pass a timer from one fragment to another?

Suppose i have a timer which uses a TextView in Fragment A. And i want to use this timer in Fragment B. How would i do it? 假设我有一个在片段A中使用TextView的计时器。我想在片段B中使用此计时器。我该怎么做? This is the code which is being used in Fragment A to set the timer... 这是片段A中用于设置计时器的代码...

private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
        long totalDuration = mp.getDuration();
        long currentDuration = mp.getCurrentPosition();

        // Displaying total duration time
        songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
        // Displaying current time
        songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));

        // Running this thread after 100 milliseconds
        mHandler.postDelayed(this, 100);
    }
}; 

Now i want to use the timer that's associated with the songCurrentDurationLabel TextView, in Fragment B as well. 现在,我也想使用与片段B中与songCurrentDurationLabel TextView关联的计时器。

How would i go about solving this? 我将如何解决这个问题?

Thanks! 谢谢!

Use timer in the host Activity and set/post results as callbacks. 在主机活动中使用计时器,并将结果设置/发布为回调。 Android UI toolkit is not thread-safe - don't access the UI from outside the UI thread. Android UI工具包不是线程安全的-请勿从UI线程外部访问UI。

Check some gotchas . 检查一些陷阱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个片段中获取价值并传递给另一个片段 - How to get value from one fragment and pass to another fragment 如何将一个片段的引用传递给另一个片段 - How to pass reference of one fragment to another fragment 如何在ViewPager中将一个片段的值传递给另一个片段? - How to pass values from one Fragment to another in a ViewPager? 如何在Android的同一活动中将日期从一个片段传递到另一个片段? - How to pass date from one fragment to another in the same activity in Android? 如何将带有其他对象数组的对象从一个片段传递到另一个片段? - How to pass an object with an array of other objects from one fragment to another? 如何使用 Bundle() 将值从 1 个片段传递到另一个片段? - How can I pass values from 1 fragment to another with Bundle()? 如何从一个片段移动到另一个片段? - How to move from one fragment to another fragment? 如何将对象形状从一个位置移动到另一位置? - How would I move an object shape from one location to another? 如何将变量从一种方法调用到另一种方法? - How would I call variables from one method to another method? 如何使用 java 将数据从片段传递到 android 中的另一个片段? - How to pass data from fragment to another fragment in android using java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM