我有三个片段。当用户在第三个片段上并滑动到第二个片段时,我希望出现一个吐司。这可能吗?
如果您想在片段可见时显示toast,则在片段中使用onResume
:
@Override
public void onResume() {
super.onResume();
//Make your toast here
}
但是,如果您对前一个片段有特别的了解,请在包含片段的Activity
中使用一个静态变量。
例如,如果活动名为MainAvtivity
:
class MainAvtivity extends Activity {
static int currentFrag = -1;
....
}
然后,在片段的onResume
方法中,执行以下操作:
@Override
public void onResume() {
super.onResume();
if(MainActivity.currentFrag == 3)
//Make toast here
MainActivity.currentFrag = <current_fragment_number>;
}