我有一个MenuFragment,它从支持包中扩展了Fragment
MenuFragment,我可以将另一个Fragment称为SettingsFragment
SettingsFragment中,我使用Switch来启用或禁用功能。
如果功能被禁用,则特定功能的按钮应
未显示在MenuFragment中,如果已启用,则应显示。

我的问题是onCreateonCreateViewonResume等。
更改MenuFragment中的某些设置后,当我返回SettingsFragment时。
MenuFragment中所做的更改生效之前,我总是必须重新启动应用程序。

设置从SettingsFragmentMenuFragment的回调是一个好主意,然后
每次更改invalidate()中的开关时,都应在MenuFragment的主要内容上调用SettingsFragment还是有更好的解决方案?

编辑

我尝试使用回调重绘Fragment,但我注意到调用invalidate()
什么也没做。它不会强制重新绘制视图...现在我想知道是否我必须
MenuFragment替换为MenuFragment以强制调用onCreate

MenuActivity

public class MenuActivity extends NavigationActivity {
    private FragmentManager fragmentManager = null;
    private MenuFragment fragment = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        this.fragmentManager = this.getSupportFragmentManager();
        this.fragment = (MenuFragment) this.fragmentManager.findFragmentByTag(FragmentTags.MENU.getValue());

        if (savedInstanceState == null) {
            this.fragment = new MenuFragment();
            this.fragmentManager.beginTransaction().replace(R.id.menu_slide_main_content, this.fragment, FragmentTags.MENU.getValue()).commit();
        }
    }

    /**
 *
 */
@Override
protected void onResume() {
    Log.v("Menu", "OnResume called");
    super.onResume();
}

/**
 *
 */
@Override
protected void onPause() {
    Log.v("Menu", "OnResume called");
    super.onPause();
}
}


MenuFragment

public class MenuFragment extends Fragment implements OnTouchListener, OnDragListener, DropCallback {
    private SessionLoginPreferences sessionLoginPreferences = null;
    private SessionLoginSingleton sessionLoginSingleton = null;
    private SessionConfigPreferences sessionConfigPreferences = null;
    private CircularLayout circleView = null;
    private ImageButton buttonMeasure = null;
    private ImageButton buttonCalc = null;
    private ImageButton buttonProfile = null;
    private ImageButton buttonRecipe = null;
    private ImageButton buttonDiagrams = null;
    private ImageButton buttonMeasureDataList = null;
    private ImageButton buttonFollower = null;
    private ImageButton buttonSettings = null;
    private ImageButton buttonLogout = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.sessionLoginPreferences = new SessionLoginPreferences(this.getActivity());
        this.sessionLoginSingleton = SessionLoginSingleton.getInstance(this.getActivity());
        this.sessionConfigPreferences = new SessionConfigPreferences(this.getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.buttonMeasure = new ImageButton(this.getActivity());
        this.buttonMeasure.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        this.buttonMeasure.setImageResource(R.drawable.ic_measure);
        this.buttonMeasure.getBackground().setAlpha(0);
        this.buttonMeasure.setOnTouchListener(this);

        this.buttonCalc = new ImageButton(this.getActivity());
        this.buttonCalc.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        this.buttonCalc.setImageResource(R.drawable.ic_calculator);
        this.buttonCalc.getBackground().setAlpha(0);
        this.buttonCalc.setOnTouchListener(this);

        this.buttonProfile = new ImageButton(this.getActivity());
        this.buttonProfile.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        this.buttonProfile.setImageResource(R.drawable.ic_profile);
        this.buttonProfile.getBackground().setAlpha(0);
        this.buttonProfile.setOnTouchListener(this);

        this.buttonRecipe = new ImageButton(this.getActivity());
        this.buttonRecipe.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        this.buttonRecipe.setImageResource(R.drawable.ic_scanner);
        this.buttonRecipe.getBackground().setAlpha(0);
        this.buttonRecipe.setOnTouchListener(this);

        this.buttonDiagrams = new ImageButton(this.getActivity());
        this.buttonDiagrams.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        this.buttonDiagrams.setImageResource(R.drawable.ic_diagrams);
        this.buttonDiagrams.getBackground().setAlpha(0);
        this.buttonDiagrams.setOnTouchListener(this);

        this.buttonMeasureDataList = new ImageButton(this.getActivity());
        this.buttonMeasureDataList.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        this.buttonMeasureDataList.setImageResource(R.drawable.ic_list);
        this.buttonMeasureDataList.getBackground().setAlpha(0);
        this.buttonMeasureDataList.setOnTouchListener(this);

        this.buttonFollower = new ImageButton(this.getActivity());
        this.buttonFollower.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        this.buttonFollower.setImageResource(R.drawable.ic_follower);
        this.buttonFollower.getBackground().setAlpha(0);
        this.buttonFollower.setOnTouchListener(this);

        this.buttonSettings = new ImageButton(this.getActivity());
        this.buttonSettings.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        this.buttonSettings.setImageResource(R.drawable.ic_settings);
        this.buttonSettings.getBackground().setAlpha(0);
        this.buttonSettings.setOnTouchListener(this);

        this.buttonLogout = new ImageButton(this.getActivity());
        this.buttonLogout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        this.buttonLogout.setImageResource(R.drawable.ic_logout);
        this.buttonLogout.getBackground().setAlpha(0);
        this.buttonLogout.setOnTouchListener(this);

        String email = this.getEmail();
        Map<String, Boolean> config = this.sessionConfigPreferences.getConfigDetails(email);

        List<View> views = new ArrayList<View>();
        views.add(this.buttonMeasure);
        views.add(this.buttonProfile);
        views.add(this.buttonSettings);
        views.add(this.buttonLogout);

        if(config.get(Globals.CONFIG_CALC)) {
            views.add(this.buttonCalc);
        }
        if(config.get(Globals.CONFIG_RECIPE)) {
            views.add(this.buttonRecipe);
        }
        if(config.get(Globals.CONFIG_DIAGRAMS)) {
            views.add(this.buttonDiagrams);
        }
        if(config.get(Globals.CONFIG_LIST)) {
            views.add(this.buttonMeasureDataList);
        }
        if(config.get(Globals.CONFIG_FOLLOWER)) {
            views.add(this.buttonFollower);
        }

        this.circleView = new CircularLayout(this.getActivity(), this, 250, views);
        this.circleView.setOnDragListener(this);

        return this.circleView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    /**
 *
 */
@Override
public void onResume() {
    Log.v("MenuFragment", "OnResume called");
    super.onResume();
}

/**
 *
 */
@Override
public void onPause() {
    Log.v("MenuFragment", "OnResume called");
    super.onPause();
}

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN : {
                ClipData data = ClipData.newPlainText("", "");
                ImageButton imageButton = (ImageButton)view;
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(imageButton);

                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
            } break;
            case MotionEvent.ACTION_UP: {
                view.performClick();
                view.setVisibility(View.VISIBLE);
            } break;
        }

        return true;
    }

    @Override
    public boolean onDrag(View view, DragEvent event) {
        switch(event.getAction()) {
            case DragEvent.ACTION_DROP : {
                View sourceView = (View) event.getLocalState();

                float sourceX = sourceView.getX();
                float sourceY = sourceView.getY();
                float dropX = event.getX() - (sourceView.getWidth() / 2);
                float dropY = event.getY() - (sourceView.getHeight() / 2);
                sourceView.setX(dropX);
                sourceView.setY(dropY);
                sourceView.setVisibility(View.VISIBLE);

                TranslateAnimation animation = new TranslateAnimation(dropX - sourceX, 0, dropY - sourceY, 0);
                animation.setDuration(300);

                sourceView.startAnimation(animation);
                sourceView.setX(sourceX);
                sourceView.setY(sourceY);
            } break;
        }

        return true;
    }

    @Override
    public boolean onDrop(View view, DragEvent event) {
        switch(event.getAction()) {
            case DragEvent.ACTION_DROP : {
                View sourceView = (View)event.getLocalState();

                sourceView.setVisibility(View.VISIBLE);

                if(sourceView.equals(this.buttonMeasure)) {
                    this.performNFCResult();
                } else if(sourceView.equals(this.buttonCalc)) {
                    this.performCalc();
                } else if(sourceView.equals(this.buttonProfile)) {
                    this.performProfile();
                } else if(sourceView.equals(this.buttonRecipe)) {
                    this.performRecipe();
                } else if(sourceView.equals(this.buttonDiagrams)) {
                    this.performDiagrams();
                } else if(sourceView.equals(this.buttonMeasureDataList)) {
                    this.performMeasureDataList();
                } else if(sourceView.equals(this.buttonFollower)) {
                    this.performFollower();
                } else if(sourceView.equals(this.buttonSettings)) {
                    this.performSettings();
                } else if(sourceView.equals(this.buttonLogout)) {
                    this.performLogout();
                }
            } break;
        }

        return true;
    }

    private void performNFCResult() {
        Intent newIntent = new Intent(this.getActivity(), MeasureDataActivity.class);
        newIntent.putExtra(Globals.KEY_NFC, false);

        this.startActivity(newIntent);
    }

    private void performCalc() {
        Intent newIntent = new Intent(this.getActivity(), CalcActivity.class);

        this.startActivity(newIntent);
    }

    private void performProfile() {
        Intent newIntent = new Intent(this.getActivity(), ProfileActivity.class);

        this.startActivity(newIntent);
    }

    private void performRecipe() {
        Intent newIntent = new Intent(this.getActivity(), RecipeActivity.class);

        this.startActivity(newIntent);
    }

    private void performDiagrams() {
        Intent newIntent = new Intent(this.getActivity(), DiagramsActivity.class);

        this.startActivity(newIntent);
    }

    private void performMeasureDataList() {
        Intent newIntent = new Intent(this.getActivity(), MeasureDataListActivity.class);

        this.startActivity(newIntent);
    }

    private void performFollower() {
        Intent newIntent = new Intent(this.getActivity(), FollowerActivity.class);

        this.startActivity(newIntent);
    }

    private void performSettings() {
        Intent newIntent = new Intent(this.getActivity(), SettingsActivity.class);

        this.startActivity(newIntent);
    }

    private void performLogout() {
        if(this.sessionLoginPreferences.isLoggedIn()) {
            this.sessionLoginPreferences.logout();
        } else if(this.sessionLoginSingleton.isLoggedIn()) {
            SessionLoginSingleton.getInstance(null).logout();
        }

        this.getActivity().finish();
    }

    private String getEmail() {
        String email = null;

        if (this.sessionLoginPreferences.isLoggedIn()) {
            Map<String, String> userData = this.sessionLoginPreferences.getUserDetails();

            email = userData.get(Globals.KEY_EMAIL);
        } else if (this.sessionLoginSingleton.isLoggedIn()) {
            email = this.sessionLoginSingleton.getEmail();
        }

        return email;
    }
}


SettingsActivity

public class SettingsActivity extends NavigationActivity {
    private FragmentManager fragmentManager = null;
    private SettingsFragment fragment = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.fragmentManager = this.getSupportFragmentManager();
        this.fragment = (SettingsFragment) this.fragmentManager.findFragmentByTag(FragmentTags.SETTINGS.getValue());

        if (savedInstanceState == null) {
            this.fragment = new SettingsFragment();
            this.fragmentManager.beginTransaction().replace(R.id.menu_slide_main_content, this.fragment, FragmentTags.SETTINGS.getValue()).commit();
        }
    }
}


SettingsFragment

public class SettingsFragment extends Fragment implements OnCheckedChangeListener {
    private SessionConfigPreferences sessionConfigPreferences = null;
    private SessionLoginPreferences sessionLoginPreferences = null;
    private SessionLoginSingleton sessionLoginSingleton = null;
    private Switch switchDiagrams = null;
    private Switch switchRecipe = null;
    private Switch switchCalc = null;
    private Switch switchList = null;
    private Switch switchFollower = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setRetainInstance(true);
        this.getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        this.sessionConfigPreferences = new SessionConfigPreferences(this.getActivity());
        this.sessionLoginPreferences = new SessionLoginPreferences(this.getActivity());
        this.sessionLoginSingleton = SessionLoginSingleton.getInstance(this.getActivity());

        this.setHasOptionsMenu(true);
    }

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

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.switchDiagrams = (Switch) view.findViewById(R.id.settings_switch_diagrams);
        this.switchDiagrams.setOnCheckedChangeListener(this);
        this.switchRecipe = (Switch) view.findViewById(R.id.settings_switch_recipe);
        this.switchRecipe.setOnCheckedChangeListener(this);
        this.switchCalc = (Switch) view.findViewById(R.id.settings_switch_calc);
        this.switchCalc.setOnCheckedChangeListener(this);
        this.switchList = (Switch) view.findViewById(R.id.settings_switch_measuredata_list);
        this.switchList.setOnCheckedChangeListener(this);
        this.switchFollower = (Switch) view.findViewById(R.id.settings_switch_follower);
        this.switchFollower.setOnCheckedChangeListener(this);
    }

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

        String email = getEmail();
        Map<String, Boolean> config = this.sessionConfigPreferences.getConfigDetails(email);

        this.switchDiagrams.setChecked(config.get(Globals.CONFIG_DIAGRAMS));
        this.switchRecipe.setChecked(config.get(Globals.CONFIG_RECIPE));
        this.switchCalc.setChecked(config.get(Globals.CONFIG_CALC));
        this.switchList.setChecked(config.get(Globals.CONFIG_LIST));
        this.switchFollower.setChecked(config.get(Globals.CONFIG_FOLLOWER));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.getActivity().finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton view, boolean checked) {
        String email = getEmail();

        switch(view.getId()) {
        case R.id.settings_switch_diagrams:
            this.sessionConfigPreferences.configure(email, Globals.CONFIG_DIAGRAMS, checked);
            break;
        case R.id.settings_switch_recipe:
            this.sessionConfigPreferences.configure(email, Globals.CONFIG_RECIPE, checked);
            break;
        case R.id.settings_switch_calc:
            this.sessionConfigPreferences.configure(email, Globals.CONFIG_CALC, checked);
            break;
        case R.id.settings_switch_measuredata_list:
            this.sessionConfigPreferences.configure(email, Globals.CONFIG_LIST, checked);
            break;
        case R.id.settings_switch_follower:
            this.sessionConfigPreferences.configure(email, Globals.CONFIG_FOLLOWER, checked);
            break;
        }
    }

    private String getEmail() {
        String email = null;

        if (this.sessionLoginPreferences.isLoggedIn()) {
            Map<String, String> userData = this.sessionLoginPreferences.getUserDetails();

            email = userData.get(Globals.KEY_EMAIL);
        } else if (this.sessionLoginSingleton.isLoggedIn()) {
            email = this.sessionLoginSingleton.getEmail();
        }

        return email;
    }
}

最佳答案

我想以下是解决此类问题的最佳方法:

/**
 *
 */
@Override
public void onBackPressed() {
    Intent intent = new Intent(this, MenuActivity.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    this.startActivity(intent);
    this.finish();
}


将上面的代码添加到SettingsActivity会导致旧的MenuActivity被杀死,并启动新的MenuActivity,然后需要添加MenuFragment并调用onCreateonCreateView等,因此视图行为就像它已更新。

这仅适用于API级别> = 5.0

较旧的API应该使用:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent intent = new Intent(this, MenuActivity.class);

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        this.startActivity(intent);
        this.finish();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}


让我知道这是否不是一个好的解决方案。如果是这样,请提供原因信息。

关于android - 可见后重画 fragment ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27500630/

10-10 17:07