-1

Hi in my code when user press back button from mainactivity he/she exit from the application but problem is when user again press application icon it resume the mainactivity where i left it..... i want to restart my app again when user click on application icon. Thanks in advance

///exit from app by pressing back button
 public void onBackPressed() {
       Log.i("HA", "Finishing");
       Intent intent = new Intent(Intent.ACTION_MAIN);
       intent.addCategory(Intent.CATEGORY_HOME);
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       startActivity(intent);
     }

}

Sam Iqbal
  • 33
  • 4

2 Answers2

0

There are three options:

1.You can override onResume() and put whatever is there in your onCreate() method inside it to do fresh restart everytime..

2.You can override onPause and kill the activity

onStop(); onCreate(getIntent().getExtras());

3.The best approach: Alternatively you can also use http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY so that your activity is restarted everytime.

public static final int FLAG_ACTIVITY_NO_HISTORY

Added in API level 1 If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.

Constant Value: 1073741824 (0x40000000)

rupesh jain
  • 3,410
  • 1
  • 14
  • 22
0

You need this

public static final int FLAG_ACTIVITY_CLEAR_TASK

Added in API level 11 If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished.

You should look here

AnkitSomani
  • 1,152
  • 8
  • 9