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 7734833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:20:58+00:00 2026-06-01T07:20:58+00:00

My app is almost complete. In the second snippet of code below, you will

  • 0

My app is almost complete. In the second snippet of code below, you will see a while loop that is temporary. The purpose of this loop is to add ten table rows to my questionContainer (soon, I will replace this with an AsyncTask that acquires similar elements from an SQL db). I put a Log.v right in the while loop, to see when it runs. It turns out, on initial launch, the while loop and the onCreate View runs just fine. But after pressing back, then reopening the app via app switcher, while onCreateView reruns, the whileloop apparently does not execute. Could anyone provide any insight?

public class Polling extends SherlockFragmentActivity {
    private ViewPager mViewPager;
    private TabsAdapter mTabsAdapter;
    private final static String TAG = "21st Polling:";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "onCreate");
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);
        setContentView(mViewPager);
        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);

        mTabsAdapter = new TabsAdapter(this, mViewPager);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.login),
                LoginFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.economics),
                EconFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.elections),
                ElectionsFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.politics),
                PoliticsFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.science),
                ScienceFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.finance),
                FinanceFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.religion),
                ReligionFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.military),
                MilitaryFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.international),
                InternationalFragment.class, null); 
        Log.v(TAG, (String)bar.getTabAt(0).getText());

    }

public static class TabsAdapter extends FragmentPagerAdapter
    implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getSupportActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }


        public int getCount() {
            return mTabs.size();
        }

        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }


        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }


        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
        }


        public void onPageScrollStateChanged(int state) {
        }


        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            mViewPager.setCurrentItem(tab.getPosition());
            //Log.v(TAG, "clicked");
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

        public void onTabReselected(Tab tab, FragmentTransaction ft) {}

        public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}

        public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}
    }
}

And the fragment that is not repopulating after pressing back to exit app, then task switcher to return in:

import com.actionbarsherlock.R;
import com.actionbarsherlock.app.SherlockFragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.support.v4.app.Fragment;

public class EconFragment extends SherlockFragment {

    private TableLayout questionContainer;
    static int pos = 0;
    private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3",
            "hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"};

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.v("Econ", "onCreateView");
        View v = inflater.inflate(R.layout.econfragment, container, false);
        questionContainer = (TableLayout) v.findViewById(R.id.questionContainer);

        int leftMargin=5;
        int topMargin=5;
        int rightMargin=5;
        int bottomMargin=5;
        while (pos < 10) {
        View question = inflater.inflate(R.layout.question, null);
        question.setId(pos);
        TextView title = (TextView) question.findViewById(R.id.questionTextView);
        title.setText(titles[pos]);
        Button charts = (Button) question.findViewById(R.id.chartsButton);
        charts.setId(pos);
        charts.setOnClickListener(chartsListener);
        TableRow tr = (TableRow) question;
        TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
        trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        tr.setLayoutParams(trParams);
        Log.v("econ", "while loop");
        questionContainer.addView(tr);
        pos++;
        }

        return v;
    }

And here’s a sample Logcat, with the following process: Launch app, scroll to tab OTHER than Econ, press BACK, reopen app, scroll to Econ tab (where the ten tablerows do not rebuild:

03-31 21:55:13.163: V/21st Polling:(1918): onCreate
03-31 21:55:13.943: D/dalvikvm(1918): GC_FOR_ALLOC freed 122K, 3% free 9149K/9347K, paused 365ms
03-31 21:55:14.044: I/dalvikvm-heap(1918): Grow heap (frag case) to 10.002MB for 1048592-byte allocation
03-31 21:55:14.173: D/dalvikvm(1918): GC_CONCURRENT freed <1K, 3% free 10172K/10439K, paused 4ms+6ms
03-31 21:55:14.283: D/dalvikvm(1918): GC_FOR_ALLOC freed 0K, 3% free 10172K/10439K, paused 35ms
03-31 21:55:14.323: I/dalvikvm-heap(1918): Grow heap (frag case) to 12.252MB for 2359312-byte allocation
03-31 21:55:14.424: D/dalvikvm(1918): GC_CONCURRENT freed 0K, 3% free 12476K/12807K, paused 4ms+3ms
03-31 21:55:14.594: V/21st Polling:(1918): Login
03-31 21:55:14.594: V/21st Polling:(1918): onResume
03-31 21:55:14.703: D/dalvikvm(1918): GC_FOR_ALLOC freed 1053K, 10% free 11826K/13127K, paused 37ms
03-31 21:55:14.813: D/Econ(1918): onstart
03-31 21:55:14.813: D/Econ(1918): onresume
03-31 21:55:14.813: D/Econ(1918): onAttach
03-31 21:55:14.813: D/Econ(1918): onCreate
03-31 21:55:14.813: V/Econ(1918): onCreateView
03-31 21:55:14.853: V/econ(1918): while loop
03-31 21:55:14.883: V/econ(1918): while loop
03-31 21:55:15.003: V/econ(1918): while loop
03-31 21:55:15.113: V/econ(1918): while loop
03-31 21:55:15.113: D/dalvikvm(1918): GC_CONCURRENT freed 291K, 6% free 12387K/13127K, paused 4ms+5ms
03-31 21:55:15.154: V/econ(1918): while loop
03-31 21:55:15.173: V/econ(1918): while loop
03-31 21:55:15.204: V/econ(1918): while loop
03-31 21:55:15.223: V/econ(1918): while loop
03-31 21:55:15.253: V/econ(1918): while loop
03-31 21:55:15.273: V/econ(1918): while loop
03-31 21:55:15.413: D/Econ(1918): onActivityCreated
03-31 21:55:15.413: D/Econ(1918): OnStart
03-31 21:55:15.413: V/Econ(1918): onResume
03-31 21:55:16.063: D/gralloc_goldfish(1918): Emulator without GPU emulation detected.
03-31 21:55:23.496: D/Econ(1918): onpause
03-31 21:55:23.514: D/Econ(1918): onstop
03-31 21:55:23.523: D/Econ(1918): ondestroyview
03-31 21:55:24.023: D/Econ(1918): onpause
03-31 21:55:24.033: D/Econ(1918): onpause
03-31 21:55:24.093: W/IInputConnectionWrapper(1918): showStatusIcon on inactive InputConnection
03-31 21:55:24.683: D/Econ(1918): onstop
03-31 21:55:24.683: D/Econ(1918): ondestroy
03-31 21:55:24.683: D/Econ(1918): ondetach
03-31 21:55:24.683: D/Econ(1918): ondestroyview
03-31 21:55:24.763: D/Econ(1918): ondestroy
03-31 21:55:24.763: D/Econ(1918): ondetach
03-31 21:55:27.913: V/21st Polling:(1918): onCreate
03-31 21:55:28.833: V/21st Polling:(1918): Login
03-31 21:55:28.833: V/21st Polling:(1918): onResume
03-31 21:55:28.993: D/Econ(1918): onstart
03-31 21:55:28.993: D/Econ(1918): onresume
03-31 21:55:28.993: D/Econ(1918): onAttach
03-31 21:55:28.993: D/Econ(1918): onCreate
03-31 21:55:29.013: V/Econ(1918): onCreateView
03-31 21:55:29.013: D/Econ(1918): onActivityCreated
03-31 21:55:29.013: D/Econ(1918): OnStart
03-31 21:55:29.013: V/Econ(1918): onResume
  • 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-01T07:20:59+00:00Added an answer on June 1, 2026 at 7:20 am

    Your variable pos is declared as static. So even though a new Fragment instance is created, the static variable keeps it’s value (in your case 10), and so the while loop in the second occurence is skipped.

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

Sidebar

Related Questions

I am trying to build this app which is almost complete now. I am
I have to understand this code to create my own app(almost based on this
We have a web app that we update and release almost daily. We use
I know almost nothing about linq. I'm doing this: var apps = from app
I used to have a small chat app(which was almost working), that uses PHP,
I have an almost complete simple web app written as a Python CGI script.
My project that I've spent almost 40hrs on is almost complete, but I have
I am building out a messaging system for my app that is almost exactly
I have used this tutorial from Google to build a web app that finds
I have an app which acts almost like a daemon. Sometimes it quits on

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.