为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕妹3146593 2019-12-09 15:42:46
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在清单文件中声明。我需要销毁并重新创建活动,以便可以在横向模式下使用不同的布局。
查看完整描述

3 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

您每次在活动中打开屏幕时都在创建一个新的片段,onCreate();但是您也要使用来维护旧的片段super.onCreate(savedInstanceState);。因此,也许设置标签并找到片段(如果存在),或者将空束传递给super。


这花了我一些时间来学习,当您使用诸如viewpager之类的东西时,它确实可以说是一个烂摊子。


我建议您多花一些时间阅读有关片段的内容,因为涉及到了这个确切的主题。


这是一个如何在规则的方向变化中处理碎片的示例:


活动内容:


public class MainActivity extends FragmentActivity {


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        if (savedInstanceState == null) {

            TestFragment test = new TestFragment();

            test.setArguments(getIntent().getExtras());

            getSupportFragmentManager().beginTransaction().replace(android.R.id.content, test, "your_fragment_tag").commit();

        } else {

            TestFragment test = (TestFragment) getSupportFragmentManager().findFragmentByTag("your_fragment_tag");

        }

    }

}

片段:


public class TestFragment extends Fragment {


    public static final String KEY_ITEM = "unique_key";

    public static final String KEY_INDEX = "index_key";

    private String mTime;


    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_layout, container, false);


        if (savedInstanceState != null) {

            // Restore last state

            mTime = savedInstanceState.getString("time_key");

        } else {

            mTime = "" + Calendar.getInstance().getTimeInMillis();

        }


        TextView title = (TextView) view.findViewById(R.id.fragment_test);

        title.setText(mTime);


        return view;

    }


    @Override

    public void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);

        outState.putString("time_key", mTime);

    }

}


查看完整回答
反对 回复 2019-12-09
?
30秒到达战场

TA贡献1828条经验 获得超6个赞

可以在android准则中找到有关如何在方向变化和活动娱乐之间保留数据的良好准则。


摘要:


使您的片段可保留:


setRetainInstance(true);

仅在必要时创建新片段(或至少从中获取数据)


dataFragment = (DataFragment) fm.findFragmentByTag("data");


// create the fragment and data the first time

if (dataFragment == null) {


查看完整回答
反对 回复 2019-12-09
  • 3 回答
  • 0 关注
  • 424 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信