gpt4 book ai didi

android - 不要在屏幕旋转时调用方法

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:51 25 4
gpt4 key购买 nike

我有一个名为 OpFragment 的父 fragment 。从这个 fragment 继承了我应用程序中的所有 fragment 。

public abstract class OpFragment extends Fragment {

private Loading loading;

public abstract int getLayoutId();


public abstract void getData();


public abstract void setListeners();
protected BackHandlerInterface backHandlerInterface;

public boolean onBackPressed(){
return false;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if(!(getActivity() instanceof BackHandlerInterface)) {
throw new ClassCastException("Hosting activity must implement BackHandlerInterface");
} else {
backHandlerInterface = (BackHandlerInterface) getActivity();
}
FragmentArgs.inject(this);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(getLayoutId(), container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if(!isAdded()){
return;
}
getData();
setListeners();

}

protected String returnName() {
return null;
}

public void showTitle() {
EventBus.getDefault().post(new ShowName(returnName()));
}

@Override
public void onDestroyView() {
super.onDestroyView();
}

public Loading getLoading() {
if (this.loading == null) {
this.loading = new Loading(this.getActivity());
}
return this.loading;
}

/**
* Gets a component for dependency injection by its type.
*/
@SuppressWarnings("unchecked")
protected <C> C getComponent(Class<C> componentType) {
return componentType.cast(((HasComponent<C>) getActivity()).getComponent());
}

@Override
public void onStart() {
super.onStart();
backHandlerInterface.setSelectedFragment(this);
}

public interface BackHandlerInterface {
void setSelectedFragment(OpFragment backHandledFragment);
}

@Override
public void onResume() {
super.onResume();
sendGAScreens();
}

private void sendGAScreens() {
final Tracker tracker = OpApp.getDefaultTracker();
if(tracker != null) {
tracker.setScreenName(getClass().getSimpleName());
tracker.send(new HitBuilders.ScreenViewBuilder().build());
}
}
}

onViewCreated 中有方法getData()setListeners()。我不想在屏幕旋转后记忆起这些方法。我怎样才能做到这一点 ?简单地检查 savedInstanceState == null 没有给我预期的结果。

最佳答案

覆盖此方法以检测 fragment 中的屏幕旋转,并在屏幕旋转时设置一些标志。如下图:

@Override 
public void onConfigurationChanged(Configuration newConfig)
{
Log.d("tag", "config changed");
super.onConfigurationChanged(newConfig);

int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT || orientation == Configuration.ORIENTATION_LANDSCAPE)
flag= true;

....
}

并在您的 onViewCreated() 中这样做

if(!flag){
// call your functions
}

关于android - 不要在屏幕旋转时调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37964305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com