暂停和恢复无法正常工作

时间:2014-02-05 05:22:10

标签: android onresume onpause

在我的应用程序第一次启动应用程序时,我按设备上的主页按钮。然后从头开始再次启动它。之后我每次启动它并按Home键然后再次启动它从我离开它的同一点开始。如何解决这个第一次运行错误?

代码 -

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        textView1 = (TextViewEx) findViewById (R.id.textView1);
        textView1.setText("Text", true);
        textView1.setMovementMethod(new ScrollingMovementMethod());
    }

    public void onBackPressed()  
    {    
        intent = new Intent(History.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
            finish(); 
    }  

    @Override
    protected void onPause() {
        super.onPause();
        objIntent = new Intent(History.this, PlayAudio.class);
        stopService(objIntent);
    }

    @Override 
    public void onResume()
    {
        super.onResume();
        objIntent = new Intent(History.this, PlayAudio.class);
            startService(objIntent);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }    
}

1 个答案:

答案 0 :(得分:0)

试试这个

private boolean paused = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.history);
    textView1 = (TextViewEx) findViewById (R.id.textView1);
    textView1.setText("Text", true);
    textView1.setMovementMethod(new ScrollingMovementMethod());
}

...

@Override
protected void onPause() {
    super.onPause();
    paused = true;
}

@Override 
public void onResume()
{
    // On resume loads every times when activity starts after onCreate
    super.onResume();
    if(paused){
        objIntent = new Intent(History.this, PlayAudio.class);
        startService(objIntent);
    }
}