0

In my app when app start for the first time and I press Home button from device. And then start it again its start from beginning. After that each time I start it and press Home key and then start it again its start from same point where I leave it. How to solve this first time run error?

Code-

@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();
    }    
}
John R
  • 2,078
  • 8
  • 35
  • 58
  • So its not behaving properly *only* the first time you press home button? – SoulRayder Feb 05 '14 at 05:26
  • you need to save the state and data in shared preference and each time start load previous state – mohammed momn Feb 05 '14 at 05:27
  • 1
    This is a known issue in android... https://code.google.com/p/android/issues/detail?id=2373#c40 and see here http://stackoverflow.com/questions/17000097/fail-to-resume-the-activity-stack-during-1st-launching-after-installation and here http://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time – Gopal Gopi Feb 05 '14 at 05:28
  • @Gautham yes in first time. – John R Feb 05 '14 at 05:31
  • @GopalRao can you help me on this please http://stackoverflow.com/questions/21569347/navigating-up-and-overflow-icon-in-action-bar – John R Feb 05 '14 at 06:27
  • @JohnR have you solved that Overflow menu problem? – Gopal Gopi Feb 05 '14 at 09:07
  • @GopalRao no please see http://stackoverflow.com/questions/21569347/overflow-icon-is-not-shown – John R Feb 05 '14 at 09:08
  • @GopalRao please post your answer including links you send me for this question. This problem solved. – John R Feb 05 '14 at 09:10
  • @JohnR Glad to help you and let the comment to be a comment. Thank you... – Gopal Gopi Feb 05 '14 at 09:22

1 Answers1

0

Try this one

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);
    }
}
Milind
  • 83
  • 6