旋转屏幕时不要调用方法。

3
我有一个名为OpFragment的父片段。所有应用程序中的片段都继承自该片段。
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并不能达到预期的结果。

1个回答

4

重写此方法以检测您的片段中的屏幕旋转,并在屏幕旋转时设置一些标志,如下所示:

@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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接