我想知道如何知道在將方向切換爲風景之前,我的片段(啓動畫面)已經顯示。我想在改變方向之後,活動被破壞了。片段 - 更改方向
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擴展片段{從我的評論
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);
}
您的SplashFragment的處理程序完成後,您可以在捆綁中添加布爾值。因此,當調用SplashFragment時,savedInstanceState可以檢查它是否爲true,然後跳過並顯示ContactListFragment。 或者使用SharedPreferences並將布爾值存儲在那裏,並從MainActivity中檢查它並執行if()else {} showFragment(new SplashFragment.newInstance())或showFragment(newContactListFragment.newInstance()) –
爲什麼不實現splashscreen作爲活動主題並擺脫splashscreen碎片,因此您可以確保在您輸入主要活動之前顯示splashscreen?之後,你將從配置中獲得定位。請在面向風景的應用程序中定義你想要肖像閃屏的結果? –