尽管onCreateView再次运行,View不正确回来后preSS重组? [英] Even though onCreateView runs again, View does not properly reassemble after BACK press?

查看:223
本文介绍了尽管onCreateView再次运行,View不正确回来后preSS重组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序已接近完成。在下面code的第二片断,你会看到一个while循环是暂时的。这个循环的目的是十人一桌行添加到我的questionContainer(不久,我将与从SQL数据库获取类似元素的AsyncTask的替换此)。我把Log.v权while循环,看到它运行时。事实证明,在最初推出时,while循环和的onCreate查看运行良好。但经过pressing回来,然后重新打开通过软件切换应用程序,而onCreateView重播时,while循环显然不执行。任何人都可以提供任何见解?

 公共类轮询扩展SherlockFragmentActivity {
    私人ViewPager mViewPager;
    私人TabsAdapter mTab​​sAdapter;
    私人最终静态字符串TAG =21轮询:    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.v(TAG的onCreate);
        mViewPager =新ViewPager(本);
        mViewPager.setId(R.id.pager);
        的setContentView(mViewPager);
        动作条酒吧= getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowTitleEnabled(假);
        bar.setDisplayShowHomeEnabled(假);        mTabsAdapter =新TabsAdapter(这一点,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,(字符串)bar.getTabAt(0).getText());    }公共静态类TabsAdapter扩展FragmentPagerAdapter
    实现ActionBar.TabListener,ViewPager.OnPageChangeListener {
        私人最终上下文mContext;
        私人最终动作条mActionBar;
        私人最终ViewPager mViewPager;
        私人最终的ArrayList< TabInfo> mTabs =新的ArrayList< TabInfo>();        静态final类TabInfo {
            私人最终级<> CLSS;
            私人最终捆绑ARGS;            TabInfo(类<> _class,捆绑_args){
                CLSS = _class;
                ARGS = _args;
            }
        }        公共TabsAdapter(SherlockFragmentActivity活动,ViewPager寻呼机){
            超级(activity.getSupportFragmentManager());
            mContext =活动;
            mActionBar = activity.getSupportActionBar();
            mViewPager =寻呼机;
            mViewPager.setAdapter(本);
            mViewPager.setOnPageChangeListener(本);
        }        公共无效addTab(ActionBar.Tab标签,类<> CLSS,捆绑参数){
            TabInfo信息=新TabInfo(CLSS,参数);
            tab.setTag(信息);
            tab.setTabListener(本);
            mTabs.add(信息);
            mActionBar.addTab(标签);
            notifyDataSetChanged();
        }
        公众诠释的getCount(){
            返回mTabs.size();
        }        公共片段的getItem(INT位置){
            TabInfo信息= mTab​​s.get(位置);
            返回Fragment.instantiate(mContext,info.clss.getName(),info.args);
        }
        公共无效onPageScrolled(INT位置,浮positionOffset,诠释positionOffsetPixels){
        }
        公共无效使用onPageSelected(INT位置){
            mActionBar.setSelectedNavigationItem(位置);
        }
        公共无效onPageScrollStateChanged(INT状态){
        }
        公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){
            mViewPager.setCurrentItem(tab.getPosition());
            //Log.v(TAG,点击);
            对象标记= tab.getTag();
            的for(int i = 0; I< mTab​​s.size();我++){
                如果(mTabs.get(ⅰ)==标签){
                    mViewPager.setCurrentItem(ⅰ);
                }
            }
        }        公共无效onTabUnselected(标签选项卡,FragmentTransaction英尺){}        公共无效onTabReselected(标签选项卡,FragmentTransaction英尺){}        公共无效onTabReselected(标签选项卡,android.app.FragmentTransaction英尺){}        公共无效onTabUnselected(标签选项卡,android.app.FragmentTransaction英尺){}
    }
}

和未经过pressing再植回退出程序片段,然后任务切换,以返回:

 进口com.actionbarsherlock.R;
进口com.actionbarsherlock.app.SherlockFragment;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup.LayoutParams;
进口android.view.inputmethod.InputMethodManager;
进口android.widget.Button;
进口android.widget.LinearLayout;
进口android.widget.RelativeLayout;
进口android.widget.ScrollView;
进口android.widget.TableLayout;
进口android.widget.TableRow;
进口android.widget.TextView;
进口android.support.v4.app.Fragment;公共类EconFragment扩展SherlockFragment {    私人TableLayout questionContainer;
    静态INT POS = 0;
    私有String []标题= {第一题,hallo1,hallo2,hallo3
            hallo4,hallo5,hallo6,hallo7,hallo8,hallo9};    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        Log.v(经济舱,onCreateView);
        视图V = inflater.inflate(R.layout.econfragment,集装箱,FALSE);
        questionContainer =(TableLayout)v.findViewById(R.id.questionContainer);        INT LEFTMARGIN = 5;
        INT TOPMARGIN = 5;
        INT rightMargin = 5;
        INT bottomMargin = 5;
        而(正小于10){
        查看问题= inflater.inflate(R.layout.question,NULL);
        question.setId(POS)
        TextView的标题=(TextView中)question.findViewById(R.id.questionTextView);
        title.setText(标题[POS]);
        按钮图表=(按钮)question.findViewById(R.id.chartsButton);
        charts.setId(POS)
        charts.setOnClickListener(chartsListener);
        TR的TableRow =(的TableRow)的问题;
        TableLayout.LayoutParams trParams =新TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
        trParams.setMargins(LEFTMARGIN,TOPMARGIN,rightMargin,bottomMargin);
        tr.setLayoutParams(trParams);
        Log.v(经济学,while循环);
        questionContainer.addView(TR);
        POS ++;
        }        返回伏;
    }

这是一个示例logcat中,有以下过程:启动应用程序,滚动到标签不是经济学,preSS BACK其他,重新打开应用程序,滚动到经济学标签(凡十tablerows不重建:

  03-31 21:55:13.163:V / 21轮询:(1918):的onCreate
03-31 21:55:13.943:D / dalvikvm(1918年):GC_FOR_ALLOC释放122K,3%的自由9149K / 9347K,暂停365ms
03-31 21:55:14.044:I / dalvikvm堆(1918年):成长堆(frag的情况下),以10.002MB为1048592字节分配
03-31 21:55:14.173:D / dalvikvm(1918):GC_CONCURRENT释放< 1K,免费10172K / 10439K 3%,暂停为4ms + 6ms的
03-31 21:55:14.283:D / dalvikvm(1918):GC_FOR_ALLOC释放0K,免费3%10172K / 10439K,暂停为35ms
03-31 21:55:14.323:I / dalvikvm堆(1918年):成长堆(frag的情况下),以12.252MB为2359312字节分配
03-31 21:55:14.424:D / dalvikvm(1918年):GC_CONCURRENT释放0K,免费3%12476K / 12807K,暂停为4ms + 3ms的
03-31 21:55:14.594:V / 21轮询:(1918):登录
03-31 21:55:14.594:V / 21轮询:(1918):onResume
03-31 21:55:14.703:D / dalvikvm(1918年):GC_FOR_ALLOC释放1053K,10%免费11826K / 13127K,暂停37ms
03-31 21:55:14.813:D /经济学(1918):的OnStart
03-31 21:55:14.813:D /经济学(1918):onresume
03-31 21:55:14.813:D /经济学(1918):onAttach
03-31 21:55:14.813:D /经济学(1918)的onCreate
03-31 21:55:14.813:V /经济舱(1918):onCreateView
03-31 21:55:14.853:V /经济学(1918):while循环
03-31 21:55:14.883:V /经济学(1918):while循环
03-31 21:55:15.003:V /经济学(1918):while循环
03-31 21:55:15.113:V /经济学(1918):while循环
03-31 21:55:15.113:D / dalvikvm(1918年):GC_CONCURRENT释放291K,免费6%12387K / 13127K,暂停为4ms + 5ms的
03-31 21:55:15.154:V /经济学(1918):while循环
03-31 21:55:15.173:V /经济学(1918):while循环
03-31 21:55:15.204:V /经济学(1918):while循环
03-31 21:55:15.223:V /经济学(1918):while循环
03-31 21:55:15.253:V /经济学(1918):while循环
03-31 21:55:15.273:V /经济学(1918):while循环
03-31 21:55:15.413:D /经济学(1918):onActivityCreated
03-31 21:55:15.413:D /经济学(1918):的OnStart
03-31 21:55:15.413:V /经济舱(1918):onResume
03-31 21:55:16.063:D / gralloc_goldfish(1918年):无仿真模拟GPU检测。
03-31 21:55:23.496:D /经济学(1918):的onPause
03-31 21:55:23.514:D /经济学(1918):的onStop
03-31 21:55:23.523:D /经济学(1918):ondestroyview
03-31 21:55:24.023:D /经济学(1918):的onPause
03-31 21:55:24.033:D /经济学(1918):的onPause
03-31 21:55:24.093:W / IInputConnectionWrapper(1918):showStatusIcon上的非活动InputConnection
03-31 21:55:24.683:D /经济学(1918):的onStop
03-31 21:55:24.683:D /经济学(1918):的OnDestroy
03-31 21:55:24.683:D /经济学(1918):ondetach
03-31 21:55:24.683:D /经济学(1918):ondestroyview
03-31 21:55:24.763:D /经济学(1918):的OnDestroy
03-31 21:55:24.763:D /经济学(1918):ondetach
03-31 21:55:27.913:V / 21轮询:(1918):的onCreate
03-31 21:55:28.833:V / 21轮询:(1918):登录
03-31 21:55:28.833:V / 21轮询:(1918):onResume
03-31 21:55:28.993:D /经济学(1918):的OnStart
03-31 21:55:28.993:D /经济学(1918):onresume
03-31 21:55:28.993:D /经济学(1918):onAttach
03-31 21:55:28.993:D /经济学(1918)的onCreate
03-31 21:55:29.013:V /经济舱(1918):onCreateView
03-31 21:55:29.013:D /经济学(1918):onActivityCreated
03-31 21:55:29.013:D /经济学(1918):的OnStart
03-31 21:55:29.013:V /经济舱(1918):onResume


解决方案

您变量 POS 被声明为静态的。因此,即使是创造了一个新片段的实例,静态变量保持它的价值(在你的案件10​​),因此,在第二occurence while循环被跳过。

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

解决方案

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.

这篇关于尽管onCreateView再次运行,View不正确回来后preSS重组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆