我想知道在将切换方向转换为横向之前,我的片段(启动画面)已经显示出来了。我想在方向改变后活动被破坏了。
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar ...
showFragment(new SplashFragment());
}
public void showFragment(Fragment fragment) {
getSupportFragmentManager().beginTransaction().replace(R.id.main_frame_layout, fragment).addToBackStack("").commit();
}
@Override
//Menu item icons
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
片段(闪屏)
公共类SplashFragment扩展了Fragment {
public static SplashFragment newInstance() {
Bundle args = new Bundle();
SplashFragment fragment = new SplashFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_splash, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
((MainActivity) getActivity()).getSupportActionBar().hide();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
((MainActivity) getActivity()).showFragment(ContactListFragment.newInstance());
}
}, 1000);
}
答案 0 :(得分:0)
引用我的评论
在为SplashFragment完成处理程序后,您可以在包中添加一个布尔值。因此,当调用SplashFragment时,savedInstanceState可以检查它是否为true,然后跳过并显示ContactListFragment。或者使用SharedPreferences并将boolean存储在那里并从MainActivity中检查它并对showFragment(new SplashFragment.newInstance())或showFragment(newContactListFragment.newInstance())执行if()else {}
SharedPreferences示例:
MainActivity.java
在showFragment()
之前的onCreate
[...] // other activity stuff
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
boolean hasShown = sharedPref.getBoolean("hasShown", false);
if(hasShown)
{ showFragment(new ContactListFragment()); }
else
{ showFragment(new SplashFragment()); }
SplashFragment.java
在new Handler()
之前的onViewCreated
SharedPreferences sharedPref = ((MainActivity) getActivity()).getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("hasShown", true);
editor.commit();
答案 1 :(得分:0)
到了你打电话的地步
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
您可以通过调用
从FragmentManager
找到您的片段
getFragmentManager().findFragmentByTag(/* by id or tag */)
例如你可以快速告诉
显示的片段getFragmentManager().findFragmentByTag(/*Id or TAG*/) != null ? fragment was added to FragmentManager : fragment is not in FragmentManager
如果您调用了remove()方法,则无法知道是否显示了splash。您可以尝试创建一个布尔值系统来指示片段是否在FragmentManager中。
Android上的方向更改会杀死活动和碎片。通过正确保存您的活动和碎片状态
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState)
//Save your stuff to bundle
}
并在
恢复它们@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( savedInstanceState == null) return;
//get your stuff from savedInstanceState
//Example
boolean[] array = savedInstanceState.getBooleanArray("yourkey");
}
您可以将布尔指示符的示例保存到boolean []数组,并知道在此“全局”生命周期中发生了什么