Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:59:02+00:00 2026-06-14T06:59:02+00:00

I have two fragments Fragment1 and Fragment2.When I start the application the functions defined

  • 0

I have two fragments Fragment1 and Fragment2.When I start the application the functions defined inside both of the fragments gets executed.So it takes a long time on startup(Since XML parsing and population of list view are executed on both fragments).I need to reduce the startup time of the application.Is their any way to execute Fragment1 on startup, and execution of Fragment2 bring to background? So that the user can interact with Fragment1.And Fragment2 execute its functions without diturbing the user.

Here is my code,

Fragment1=>

    public class Fragment1 extends Fragment{


public static String feedurl="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
static String URL = "";
static final String KEY_HEAD = "item"; // parent node
static final String KEY_DATE = "pubDate";
public static String headflag="";
int f=0;
GridView list;
HeadlinesAdapter adapter;
private TextView mMessageView;
private Button mClearButton;

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

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


    return v;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);



    headlines.parse();
    populate_listview();

    }


 public void populate_listview()
 {


     URL="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
     ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL);
    Document doc = parser.getDomElement(xml);
    NodeList nl = doc.getElementsByTagName(KEY_HEAD);
    NodeList itemLst = doc.getElementsByTagName("item");
    String MarqueeStr="";

    for (int i = 0; i < nl.getLength(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
                    newsList.add(map);


 }
    list=(GridView)getActivity().findViewById(R.id.grid);
    adapter=new Adapter1(getActivity(), newsList);        
            list.setAdapter(adapter);

  }

Fragment2=>

    public class Fragment2 extends Fragment{


public static String feedurl="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
static String URL = "";
static final String KEY_HEAD = "item"; // parent node
static final String KEY_DATE = "pubDate";
public static String headflag="";
int f=0;
GridView list;
HeadlinesAdapter adapter;
private TextView mMessageView;
private Button mClearButton;

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

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


    return v;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);



    sports.parse();
    populate_listview();

    }


 public void populate_listview()
 {


     URL="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
     ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL);
    Document doc = parser.getDomElement(xml);
    NodeList nl = doc.getElementsByTagName(KEY_HEAD);
    NodeList itemLst = doc.getElementsByTagName("item");
    String MarqueeStr="";

    for (int i = 0; i < nl.getLength(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
                newsList.add(map);


 }
    list=(GridView)getActivity().findViewById(R.id.grid2);
    adapter=new HeadlinesAdapter(getActivity(), newsList);        
            list.setAdapter(adapter);

  }

MainActivity=>

 public class MainActivity extends FragmentActivity {

private ViewPager mViewPager;
private MessageLoader mLoader;
private Button mSenderButton, mReceiverButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // We get UI references
    mViewPager = (ViewPager) findViewById(R.id.viewPager);
    mSenderButton = (Button) findViewById(R.id.sender_button);
    mReceiverButton = (Button) findViewById(R.id.receiver_button);
    // set pager adapter
    mViewPager.setAdapter(new MyAdapter(this));
    // set receiver button listener
    mReceiverButton.setOnClickListener(new OnClickListener() {          

        public void onClick(View v) {
            mViewPager.setCurrentItem(1);
        }
    });

    mSenderButton.setOnClickListener(new OnClickListener() {            

        public void onClick(View v) {
            mViewPager.setCurrentItem(0);
        }
    });
}


private class MyAdapter extends FragmentPagerAdapter{

    private Context mContext;
    private String[] frags = {Fragment1.class.getName(), Fragment2.class.getName()};

    public MyAdapter(FragmentActivity activity) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment frag = (Fragment) super.instantiateItem(container, position);
        if(frag instanceof MessageLoader){
            mLoader = (MessageLoader) frag;
        }
        return frag;
    }

    @Override
    public Fragment getItem(int pos) {
        return Fragment.instantiate(mContext, frags[pos]);
    }

    @Override
    public int getCount() {
        return frags.length;
    }

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:59:03+00:00Added an answer on June 14, 2026 at 6:59 am

    This is related to your othewr questions:

    Is their any way to stop execution of Fragment2 when application
    startup.I have to start it only when I swipe to Fragment2

    No and you shouldn’t try to do this in the first place. The ViewPager needs the second Fragment(which isn’t visible yet) to be in good shape so the user can immediately swipe to it if he chooses. If that Fragment wouldn’t be built then the user experience will be poor especially in your case as you parse some xml from an Url on the main UI thread. You should really look at doing that xml parsing on a background thread if you want to improve the speed of your app and avoid possible ANR’s.

    I want to call ‘function1’ on startup and ‘function2’ only when I
    swipe to Fragment2

    Leave Fragment 1 as it is now(calling that method you wish to be called) and then use the OnPageChangeListener on the ViewPager. I assume that you use a custom FragmentPagerAdapter so you could get a reference to the second Fragment and directly call the populate_listview() method:

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
          Fragment2 = (Fragment2) getSupportFragmentManager()
                        .findFragmentByTag(
                                "android:switcher:" + R.id.viewPager + ":" + 1);
          if (fragment != null) {
            fragment.populate_listview();// remove the call from onViewCreated
          }
    }
    

    But this is just a hack. What you want to do is to use background threads(AysncTask or normal threads) for the long operation. Have a look at this blog entry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my android application Iam using viewpager.I have two fragments.Fragment1 and Fragment2.Iam calling two
I have a layout with two fragments and both fragments have their own action
I have two fragments in an activity where one fragment takes up 70% and
I have a ViewPager with 3 fragments into it. Everything works fine with two
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I am currently working on an Android tablet application which has two fragments, a
I have two fragments: 1st fragment consists of a large button in the middle
I have an activity made up of two fragments. On the left is a
I have two tabs hosted in my Main Activity (I use ActionBarSherlock). My fragments
I have two fragments in an activity: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=horizontal android:id=@+id/mainlayout android:layout_width=fill_parent android:layout_height=fill_parent> <fragment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.