FragmentBasics的执行流程在http://developer.android.com/training/basics/fragments/creating.html

时间:2016-02-29 19:38:46

标签: android xml android-fragments

也许我不知道关于Android应用开发和xml的第一件事(但我在Google Play商店有3个应用),但究竟是什么决定了什么时候执行?

为了尝试回答这个问题,我在Log中的每个类文件的每个方法中插入了FragmentBasics个语句,这是从标题中的URL下载的文件。

项目结构:

enter image description here

我很惊讶地看到执行顺序是什么。

    MainActivity:      `````onCreate
    HeadlinesFragment: `````onAttach
    HeadlinesFragment: `````onCreate
    AbsListView: checkAbsListViewlLogProperty get invalid command
    ArticleFragment: `````onCreateView 
    MainActivity: `````fragment_container IS null--two-pane mode
    HeadlinesFragment: `````onStart
    ArticleFragment: `````onStart

onAttachonCreate中的HeadlinesFragmentonCreateView以及ArticleFragmentMainActivty执行HeadlinesFragment"片段容器之前的原因/方式是空的......"?

为什么onCreateView没有像ArticleFragment这样的onCreate,而只是setContentView

我读到HeadlinesFragment执行此操作:"从布局资源设置活动内容。资源将被夸大,将所有顶级视图添加到活动中。"因为我正在使用"大"设备(平板电脑)和ArticleFragment以及large\news_articles.xmlxml的一部分,是否public class MainActivity extends FragmentActivity implements HeadlinesFragment.OnHeadlineSelectedListener { public void onCreate(Bundle savedInstanceState) { Log.w("MainActivity","`````onCreate"); super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); // either normal or large if (findViewById(R.id.fragment_container) == null) Log.w("MainActivity","`````fragment_container IS null--two-pane mode"); else { Log.w("MainActivity","`````fragment_container not null--one-pane mode"); if (savedInstanceState != null) return; HeadlinesFragment firstFragment = new HeadlinesFragment(); firstFragment.setArguments(getIntent().getExtras()); getSupportFragmentManager() .beginTransaction() .add(R.id.fragment_container, firstFragment) .commit(); } } } 导致类被实例化?

我真的觉得我对Android开发几乎一无所知。但我有这三个应用程序......

这里提到的3个类的本质是:

public class HeadlinesFragment extends ListFragment
{
    public void onCreate(Bundle _savedInstanceState)
    {
        Log.w("HeadlinesFragment","`````onCreate");
        super.onCreate(_savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
    }

    public void onStart()
    {
        Log.w("HeadlinesFragment","`````onStart");
        super.onStart();
        if (getFragmentManager().findFragmentById(R.id.article_fragment) != null)
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

    public void onAttach(Activity _activity)
    {
        Log.w("HeadlinesFragment", "`````onAttach");
        super.onAttach(_activity);
    }
}

public class ArticleFragment extends Fragment
{
    public View onCreateView(LayoutInflater _inflater, ViewGroup _container, Bundle _savedInstanceState)
    {
        Log.w("ArticleFragment", "`````onCreateView ");
        View v = _inflater.inflate(R.layout.article_view, _container, false);
        return v;
    }

    public void onStart()
    {
        Log.w("ArticleFragment","`````onStart");
        super.onStart();
    }
}

.xml

article_view.xml的本质:

<TextView android:id="@+id/article" />

news_articles.xml

<FrameLayout android:id="@+id/fragment_container" />

news_articles(large).xml

<LinearLayout> <fragment android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment"> <fragment android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment"/> </LinearLayout> [1]: http://i.stack.imgur.com/JPCZu.png

do_install_append() {
       # Remove .la files for loadable modules
       rm -f ${D}/${libdir}/rygel-${LIBV}/engines/*.la
       rm -f ${D}/${libdir}/rygel-${LIBV}/plugins/*.la
}

1 个答案:

答案 0 :(得分:0)

其他人已经和你做了同样的事情,但也建立了一个nice image,其中包含了生命周期的实际发生方式。这是图像图的direct reference