Issue
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
Solution
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.
Answered By - soren.qvist
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.