Is there a method that works like start fragment for result?

后端 未结 10 1282
孤城傲影
孤城傲影 2020-12-02 09:51

I currently have a fragment in an overlay. This is for signing in to the service. In the phone app, each of the steps I want to show in the overlay are their own screens and

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 10:03

    My 2 cents.

    I switch beween fragments by swapping an old fragment with a new one using hide and show/add (existing/new). So this answer is for devs who use fragments like I do.

    Then I use the onHiddenChanged method to know that the old fragment got switched to back from the new one. See code below.

    Before leaving the new fragment, I set a result in a global parameter to be queried by the old fragment. This is a very naive solution.

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) return;
        Result result = Result.getAndReset();
        if (result == Result.Refresh) {
            refresh();
        }
    }
    
    public enum Result {
        Refresh;
    
        private static Result RESULT;
    
        public static void set(Result result) {
            if (RESULT == Refresh) {
                // Refresh already requested - no point in setting anything else;
                return;
            }
            RESULT = result;
        }
    
        public static Result getAndReset() {
            Result result = RESULT;
            RESULT = null;
            return result;
        }
    }
    

提交回复
热议问题