【发布时间】:2016-12-09 22:59:52
【问题描述】:
我正在尝试制作一个片段,以便如果它在保持在同一页面之前已经打开,或者如果没有转到不同的片段。似乎我尝试过的任何东西都不起作用。请帮忙。
我的代码:
public class PreferencesView extends Fragment {
public static boolean Preferences_Opened = false;
public void changePreferences() {
if (!Preferences_Opened) {
PreferencesChange newFragment = new PreferencesChange();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace((What am I supposed to put here?), newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.activity_preferences_view, container, false);
changePreferences();
//Other non-important stuff here
return rootView;
}
}
【问题讨论】:
-
你能解释一下“之前已经打开过”是什么意思吗?您的意思是用户在使用应用程序的整个过程中至少打开了一次片段/屏幕?或者如果应用程序关闭并且用户在稍后的时间点回来,是否需要重置“之前已打开”布尔值?此外,对于您的
(What am I supposed to put here?),您可以将其替换为容器视图的 id(通常是您活动布局中的 FrameLayout 视图)。在此处查看答案:stackoverflow.com/questions/11620855/… -
第一个,“你的意思是用户在整个使用过程中至少打开了一次片段/屏幕?”