不要在屏幕旋转上调用方法

时间:2016-06-22 09:45:05

标签: android android-fragments

我有一个名为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());
        }
    }
}

getData()内有方法setListeners()onViewCreated。 屏幕旋转后我不想回忆这种方法。我怎样才能做到这一点 ? 只需检查savedInstanceState == null没有给我预期的结果。

1 个答案:

答案 0 :(得分:4)

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

this.responseText

并在@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()