第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

方向變化時處理片段的萬無一失的方法

方向變化時處理片段的萬無一失的方法

慕妹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}我需要并排顯示兩個列表視圖,菜單在左側,其內(nèi)容在右側。默認情況下,第一個菜單處于選中狀態(tài),其內(nèi)容顯示在右側。顯示內(nèi)容的片段如下: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被調(diào)用兩次。有很多使用片段的示例。由于我是該主題的初學者,因此無法將示例與我的問題聯(lián)系起來。我需要一種萬無一失的方法來處理方向更改。我尚未android:configChanges在清單文件中聲明。我需要銷毀并重新創(chuàng)建活動,以便可以在橫向模式下使用不同的布局。
查看完整描述

3 回答

?
GCT1015

TA貢獻1827條經(jīng)驗 獲得超4個贊

您每次在活動中打開屏幕時都在創(chuàng)建一個新的片段,onCreate();但是您也要使用來維護舊的片段super.onCreate(savedInstanceState);。因此,也許設置標簽并找到片段(如果存在),或者將空束傳遞給super。


這花了我一些時間來學習,當您使用諸如viewpager之類的東西時,它確實可以說是一個爛攤子。


我建議您多花一些時間閱讀有關片段的內(nèi)容,因為涉及到了這個確切的主題。


這是一個如何在規(guī)則的方向變化中處理碎片的示例:


活動內(nèi)容:


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秒到達戰(zhàn)場

TA貢獻1828條經(jīng)驗 獲得超6個贊

可以在android準則中找到有關如何在方向變化和活動娛樂之間保留數(shù)據(jù)的良好準則。


摘要:


使您的片段可保留:


setRetainInstance(true);

僅在必要時創(chuàng)建新片段(或至少從中獲取數(shù)據(jù))


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


// create the fragment and data the first time

if (dataFragment == null) {


查看完整回答
反對 回復 2019-12-09
  • 3 回答
  • 0 關注
  • 492 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號