方向变化时处理片段的万无一失的方法

public class MainActivity extends Activity implements MainMenuFragment.OnMainMenuItemSelectedListener {


 @Override

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    FragmentManager fragmentManager = getFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager

            .beginTransaction();


    // add menu fragment

    MainMenuFragment myFragment = new MainMenuFragment();

    fragmentTransaction.add(R.id.menu_fragment, myFragment);


    //add content

    DetailPart1 content1= new DetailPart1 ();

    fragmentTransaction.add(R.id.content_fragment, content1);

    fragmentTransaction.commit();


}

public void onMainMenuSelected(String tag) {

  //next menu is selected replace existing fragment

}

我需要并排显示两个列表视图,菜单在左侧,其内容在右侧。默认情况下,第一个菜单处于选中状态,其内容显示在右侧。显示内容的片段如下:


public class DetailPart1 extends Fragment {

  ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();

  ListAdapter adap;

  ListView listview;


  @Override

  public void onActivityCreated(Bundle savedInstanceState) {

      super.onActivityCreated(savedInstanceState);


       if(savedInstanceState!=null){

        myList = (ArrayList)savedInstanceState.getSerializable("MYLIST_obj");

        adap = new LoadImageFromArrayListAdapter(getActivity(),myList );

        listview.setAdapter(adap);

       }else{

        //get list and load in list view

        getlistTask = new GetALLListTasks().execute();

    }



     @Override

   public View onCreateView(LayoutInflater inflater, ViewGroup container,

        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.skyview_fragment, container,false);

           return v;

        }

onActivityCreated和onCreateView被调用两次。有很多使用片段的示例。由于我是该主题的初学者,因此无法将示例与我的问题联系起来。我需要一种万无一失的方法来处理方向更改。我尚未android:configChanges在清单文件中声明。我需要销毁并重新创建活动,以便可以在横向模式下使用不同的布局。


慕妹3146593
浏览 446回答 3
3回答

GCT1015

您每次在活动中打开屏幕时都在创建一个新的片段,onCreate();但是您也要使用来维护旧的片段super.onCreate(savedInstanceState);。因此,也许设置标签并找到片段(如果存在),或者将空束传递给super。这花了我一些时间来学习,当您使用诸如viewpager之类的东西时,它确实可以说是一个烂摊子。我建议您多花一些时间阅读有关片段的内容,因为涉及到了这个确切的主题。这是一个如何在规则的方向变化中处理碎片的示例:活动内容:public class MainActivity extends FragmentActivity {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; if (savedInstanceState == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TestFragment test = new TestFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.setArguments(getIntent().getExtras());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getSupportFragmentManager().beginTransaction().replace(android.R.id.content, test, "your_fragment_tag").commit();&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TestFragment test = (TestFragment) getSupportFragmentManager().findFragmentByTag("your_fragment_tag");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}片段:public class TestFragment extends Fragment {&nbsp; &nbsp; public static final String KEY_ITEM = "unique_key";&nbsp; &nbsp; public static final String KEY_INDEX = "index_key";&nbsp; &nbsp; private String mTime;&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; View view = inflater.inflate(R.layout.fragment_layout, container, false);&nbsp; &nbsp; &nbsp; &nbsp; if (savedInstanceState != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Restore last state&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTime = savedInstanceState.getString("time_key");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTime = "" + Calendar.getInstance().getTimeInMillis();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; TextView title = (TextView) view.findViewById(R.id.fragment_test);&nbsp; &nbsp; &nbsp; &nbsp; title.setText(mTime);&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onSaveInstanceState(Bundle outState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onSaveInstanceState(outState);&nbsp; &nbsp; &nbsp; &nbsp; outState.putString("time_key", mTime);&nbsp; &nbsp; }}

30秒到达战场

可以在android准则中找到有关如何在方向变化和活动娱乐之间保留数据的良好准则。摘要:使您的片段可保留:setRetainInstance(true);仅在必要时创建新片段(或至少从中获取数据)dataFragment = (DataFragment) fm.findFragmentByTag("data");// create the fragment and data the first timeif (dataFragment == null) {
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android