Aal*_*tel 5 java android android-fragments android-fragmentactivity
当我位于其中一个片段上时,我在活动中调用一个方法,然后返回我所在的同一个片段上的活动中的方法的结果,但令人惊讶的是,它给了我错误,说片段尚未添加。我分别打印 isAttached 和 isDetached 方法值及其 true 和 false。getContext 也返回 null。这怎么可能 ?
在调用方法之前,这些方法调用返回预期值。
Fragment.class
public class myFragment extends Fragment{
@Override
public void onDialogInteraction(String value) {
super.onDialogInteraction(value);
Log.d(TAG, "onDialogInteraction: value "+value);//prints correct value
Log.d(TAG, "onDialogInteraction: iaAdded "+isAdded()); false
Log.d(TAG, "onDialogInteraction: isAttahced "+isAttached());true
Log.d(TAG, "onDialogInteraction: isDetached "+isDetached());false
Log.d(TAG, "onDialogInteraction: isDetached "+getContext());null
}
}
public class MyActivity extends AppcompatActivity{
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String value = getValue(intent);
myFragmentInstance.onDialogInteraction(value);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1260 次 |
| 最近记录: |