我有一个MenuFragment
,它从支持包中扩展Fragment
。
从MenuFragment
我可以拨打另一个Fragment
SettingsFragment
。
在SettingsFragment
我使用Switch
来启用或禁用功能。
如果禁用某项功能,则应使用特定功能的按钮
不会显示在MenuFragment
中,如果已启用,则应显示。
我的问题是onCreate
,onCreateView
,onResume
等未被调用
当我更改MenuFragment
中的某些设置后,我回到SettingsFragment
。
在我所做的更改在MenuFragment
。
设置从SettingsFragment
到MenuFragment
的回调是一个好主意
每次我更改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;
}
}
答案 0 :(得分:0)
我想以下是解决这类问题的最佳解决方案:
/**
*
*/
@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
和{{1}调用},onCreate
等等,因此视图的行为就像它已更新一样。
这适用于API级别&gt; = 5.0
较旧的API应该使用:
onCreateView
如果这不是一个好的解决方案,请告诉我。如果是这样,请提供信息原因。
答案 1 :(得分:-1)
创建一个方法,在其中重新创建menuFragment,了解更改后的外观。然后在onCreateView中调用它。