1

I am currently trying to handle the exit and restart of my app.

So when I press the home button my app closes and the android home screen is visible. The app process still remains in the process list - this is how its supposed to be.

When I then go back to my app, I want to start it again just like i started it the first time. I have only 1 activity and here is what i do at the moment

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    this.finish();
}

So the activity closes properly - this leaves me with a blank activity when i resume my app.

Unfortunatly I dont know how to change the behaviour that the activity is created again.

Edit

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

    callResume++;
    if(callResume > 1)
    {
        Intent newInt = new Intent("android.intent.action.MainActivity");
        startActivity(newInt);
        this.finish();

    }
}
user3740359
  • 395
  • 1
  • 6
  • 16
  • maybe this will help:http://stackoverflow.com/questions/1397361/how-do-i-restart-an-android-activity. and this http://developer.android.com/reference/android/app/Activity.html#recreate().. hope this helps – Guy S Jun 14 '14 at 19:53
  • I have tried the suggested solutions in those threads. Nether of them works. The screen still gets black only. – user3740359 Jun 14 '14 at 20:09
  • Also i tried to call it in @Override onRestart(), still the new Activity isnt shown (black screen). Any ideas? – user3740359 Jun 14 '14 at 21:20

1 Answers1

1

Use startActivity to start a new activity before calling finish(). Start whatever activity you want. Setting the flag FLAG_ACTIVITY_NEW_TASK should make the stack right, I think.

EDIT:

I made a mistake- this needs to be done in onResume, and only if it isn't the first time onResume is called. Otherwise it will launch itself every time you try to leave the activity, which is definitely not what you want.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Intent newInt = new Intent("android.intent.action.MainActivity"); startActivity(newInt); this.finish(); } This lets my app crash if i exit it – user3740359 Jun 14 '14 at 19:12
  • ANd coming back I noticed one major problem- you'd want to do this in onResume, not in onPause. startActivity that way will launch the app you want (I've used that code to force people to a login activity), but if you do it in onPause it will infinitely loop the user to the activity you want to restart. It needs to be in onResume, and only if it isn't launching for the first time. – Gabe Sechan Jun 14 '14 at 19:14
  • Thanks for your response. I have changed the code in the way you said. Now the app doesnt crash, but the screen keeps black after going back to the app. Any ideas? – user3740359 Jun 14 '14 at 19:31
  • Could it be a problem that i want to create a new launcher activity from my actual launcher activity? – user3740359 Jun 15 '14 at 12:16
  • Turned out that i had a thread error in my activity i wanted to open. – user3740359 Jun 15 '14 at 15:46