应用程序启动后立即调用onSaveInstanceState

时间:2013-07-29 04:26:33

标签: android

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

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

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

不知道还有什么要补充。

在调用几个函数后发现了这个: GC_EXTERNAL_ALLOC释放1K,45%免费3339K / 6023K,外部5407K / 6752K,暂停87ms 初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
GC_EXTERNAL_ALLOC释放5K,45%空闲3344K / 6023K,外部7110K / 7462K,暂停81ms 初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
GC_EXTERNAL_ALLOC释放11K,45%免费3356K / 6023K,外部7982K / 8032K,暂停106ms 初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化zlib以充气
初始化膨胀状态
初始化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

1 个答案:

答案 0 :(得分:0)

正如documentation所说:

  

在活动被杀死之前调用此方法,以便在将来某个时间返回时可以恢复其状态。

因此,如果调用此方法,则并不意味着您的活动将始终被终止。为此,您使用onStop()onDestroy()方法,因此请确保将信息保存在onSaveInstanceState()中并使用正确的方法进行恢复。

相关问题