1

一旦从内存中读取所有数据或设置新数据,就会调用 onSaveInstanceState。我将 Log.e 调用放在每个函数的开头,以查看在调用 onSaveInstanceState 之前调用了什么。

是什么导致程序结束?有时我必须单击电源按钮才能移动解锁按钮,有时它会移动而无需按下电源按钮。

使用eclipse进行测试。在模拟器中工作,在手机上崩溃。

不知道还有什么要补充的。

在调用了几个函数后发现了这一点: GC_EXTERNAL_ALLOC 释放 1K,45% 释放 3339K/6023K,外部 5407K/6752K,暂停 87ms 初始化充气状态
初始化 zlib 到充气
初始化充气状态
初始化 zlib 到充气
初始化充气状态
初始化 zlib
充气state
初始化 zlib 到 inflate
初始化
zlib 到 inflate 初始化 zlib 到 inflate
初始化
zlib 到 inflate 初始化 zlib 到 inflate
初始化
zlib 到 inflate
GC_EXTERNAL_ALLOC释放 5K ,45% 空闲 3344K / 6023K ,外部 7110K /
7462K , 暂停 81ms zlib to inflate 初始化 inflate state 初始化 zlib to inflate 初始化 inflate state 初始化 zlib to inflate 初始化 inflate state 初始化 zlib to inflate














初始化充气状态
初始化 zlib 充气
初始化充气状态
初始化 zlib 充气
初始化充气状态
初始化 zlib 充气
初始化充气状态
初始化 zlib 充气
初始化充气状态
初始化 zlib 充气
初始化充气状态
初始化 zlib 充气
GC_EXTERNAL_ALLOC freed 11K, 45% free 3356K /6023K, 外部 7982K/8032K, 暂停 106ms Initializing inflate state
初始化 zlib to inflate 初始化 inflate
state初始化 zlib
to inflate
初始化膨胀状态
初始化 zlib 到膨胀
初始化膨胀状态
初始化 zlib 到膨胀
初始化膨胀状态
初始化 zlib 到膨胀
初始化膨胀状态
初始化 zlib 到膨胀
初始化膨胀状态
初始化 zlib 到膨胀

在我看来像某种循环

@Override  
    public void onSaveInstanceState(Bundle outState)
    {        
        //Store the game state  
        Log.e("onSaveInstanceState", " public void onSaveInstanceState");
        // ?? replace   
        // ?? is called when answer phone button is pushed    
        // ?? is called when red phone button is pushed   
        // ?? is called when house button is pushed   
        // ?? is called when power button is pushed
        // ?? "outState.putBundle" does not seem to do anything
        outState.putBundle(m_MyView.GetFileName(), 
                m_MyView.m_prRecord.SaveBundleData());


    }

{
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        try
        {
            Log.e("onCreate", "onCreate");
            //Debug.startMethodTracing("calc");
            //System.gc();

            setContentView(R.layout.main_Layout);

            PopupWindow popup = new PopupWindow(
                    this.getLayoutInflater().inflate(R.layout.menu_layout, null));

            m_MyView = (CMyView) findViewById(R.id.main);  
            m_MyView.SetTextView((TextView) findViewById(R.id.text));  

            Log.e("onCreate", "savedInstanceState == null " + (savedInstanceState == null));


            m_MyView.SetPopup(popup);
            // [breaks code]
            //popup.setContentView((CMenu) findViewById(R.id.menuView)); 

            if(savedInstanceState != null)
            {
                LoadBundleData(savedInstanceState);
            }
            else
            {
                try
                {
                    // ?? check if we have a saved file
                    InputStream fis = new FileInputStream(m_MyView.GetFileName() );
                    Log.e("onCreate", "fis == null " + (fis == null));
                    // ?? if fis == null then exception is thrown
                    // ?? if statement is used to remove unused variable error
                    if(fis != null)
                    {
                        if(m_MyView.m_prRecord.LoadByteData(
                                m_MyView.GetFileName()) == false)
                        {
                            Log.e("onCreate", "new");
                            // ?? all data was not loaded
                            m_MyView.SetMode(CMyView.NEW);
                            return;
                        }
                        else
                        {
                            Log.e("onCreate", "got here");
                            m_MyView.SetMode(CMyView.READY);
                            return;
                        }
                    }
                    // ?? if(fis == null) is dead code
                    /*else
                    {
                        //Log.e("onCreate", "fis == null");
                        m_MyView.setMode(CMyView.NEW);
                    }
                    */
                }
                catch(Exception e)
                {
                    //Log.e("onCreate", "Exception 1");
                    // We were just launched -- set up a new game
                    // ?? which means we need to make a Stream file to write to
                    //Log.e("onCreate", "ERROR IN public void onCreate: " + 
                        //  e.toString());
                    e.printStackTrace();
                    m_MyView.SetMode(CMyView.NEW);
                }

            }
            //Debug.stopMethodTracing();
        }
        catch (Exception e)
        {
            //Log.e("onCreate", "Exception 2");
            // this is the line of code that sends a real error message to the log
            //Log.e("ERROR", "ERROR IN public void onCreate: " + e.toString());

            // this is the line that prints out the location in
            // the code where the error occurred.
            //e.printStackTrace();

        }
    }

}

现在我完全糊涂了。我补充说:

 @Override
    public void onResume()
    public void onRestart()
     public void onRestoreInstanceState(Bundle savedInstanceState)
with this inside:
    {
        super.onResume();
        Log.e("onResume", "enter");
    }

onResume 被调用,但 onSaveInstanceState 未被调用。

?? 3个函数的覆盖和传递如何防止另一个函数被调用?

?? 又运行了两次,onSaveInstanceState 出现在 onResume 之后。onResume -> onSaveInstanceState -> onPause -> onDraw

4

1 回答 1

0

正如文档所说:

这个方法在一个活动可能被杀死之前被调用,这样当它在未来某个时间回来时它可以恢复它的状态。

因此,如果调用此方法并不意味着您的活动将始终被杀死。为此,您有onStop()onDestroy()方法,因此请确保您保存您的信息onSaveInstanceState()并以正确的方法恢复。

于 2013-07-29T07:36:58.573 回答