Android serial 13 - create startup activity class

According to the life cycle of fragments, we know that onAttach() method will be executed first, so we do some data initialization operations here, such as calling getNews() method to get several simulated news data, and completing the creation of NewsAdapter, and then loading news in onCreateView() method_ title_ Frag layout, and register the click practice for the list View of the news list. Next, in the onActivityCreated() method, we can find a news with id as news_ content_ The View of layout only appears in two page mode, which will be seen later in the layout. Then in the click event of ListView, we can judge that if the current mode is single page mode, a new activity will be started to display the news content. If the current mode is double page mode, the data in the news content fragment will be updated. ​


package com.example.fragmentbestpractice;


import java.util.ArrayList;

import java.util.List;




import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListView;


public class NewsTitleFragment extends Fragment implements OnItemClickListener{

  private ListView newsTitleListView;


  private List<News> newsList;


  private NewsAdapter adapter;


  private boolean isTwoPane;



  public void onAttach(Activity activity) {


    newsList = getNews(); //Initialize news data

    adapter = new NewsAdapter(activity,R.layout.news_item,newsList);




  public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

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

    newsTitleListView = (ListView) view.findViewById(;



    return view;




  public void onActivityCreated(Bundle savedInstanceState) {


    if(getActivity().findViewById(!= null) {

      isTwoPane = true;//Can be found news_content_layout Layout in two page mode     

    }else {

      isTwoPane = false;//can't find news_content_layout Single page layout





  public void onItemClick(AdapterView<?> parent,View view,int position,long id) {

    News news = newsList.get(position);

    if(isTwoPane) {

      //Refresh if in two page mode NewsContentFrayment Content in

      NewsContentFragment newsContentFragment = (NewsContentFragment)getFragmentManager().findFragmentById(;


    }else {

      //In case of single page mode, start directly NewsContentActivity

      NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent()); 



  private List<News> getNews(){

    List<News> newsList = new ArrayList<News>();

    News news1 = new News();

    news1.setTitle("Succeed in College as a Learning Disabled Student");

    news1.setContent("Collaget freshmen will soon learn to live with roomate,adjust to a new social scene and survive less-than-stellar.");

    News news2 = new News();

    news2.setTitle("Google Android exec poached by China's Xiaomi");

    news2.setContent("China's Xiaomi has poached a key Google executive.");


    return newsList;







3, Source code:

1. Project address


3. Blog Park:

4. welcome to WeChat official account: Fourier transform, official account number, only for learning communication, background reply, "gift package", get big data learning materials.


Tags: Java Android Fragment Google

Posted on Mon, 25 May 2020 07:53:45 -0700 by geoffjb