使用其他片段中的变量

时间:2016-02-07 14:52:19

标签: android android-dialogfragment

我有2节课。我想从另一个班级停止一个班级计时器。

Start.class

public class Start extends DialogFragments{
CountDownTimer timer1;
//rest of the code
}

Stop.class

public class Stop extends DialogFragments{
//I want to access the "CountDownTimer timer1;" here.

2 个答案:

答案 0 :(得分:0)

您可以使用LocalBroadcast或EventBus 请在Stop Fragment中编写sendBroadcast()。 并在Start Fragment中编写BroadcastReceiver。

答案 1 :(得分:0)

您可以在包含这些片段的活动中创建CountDownTimer对象。 您将通过getActivity()获取您的活动,并将其类型化为您的活动类。这样您就可以访问CountDownTimer对象了。片段与活动分离后,您的getActivity对象可以返回null。

其他方式简单的方法是让单个类使用它们的片段。这个你不会使用你的活力。