I am developing the application in which i want to close whole application on button click. I know in android we should not think about to close the application because andr
Try Like this Way this is work for me:
Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
instead of your code
Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Use this--
Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
Edited--New Answer and would work perfectly..
just taking an example...do it accordingly what your project needs--
I am taking three Activity
class A, B and C..and I have applied a close button on the view of class C Activity. If you want then by the Back button you can go to the previous Activity and when you press the close button then you would exit from apps..have a look--
public class AActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this, B.class);
startActivityForResult(i, 1);
}
@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == 5) {
finish();
}
}
}
Take next class activity--
public class B extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
Intent i = new Intent(this, C.class);
startActivityForResult(i, 2);
}
@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == 5) {
setResult(5);
finish();
}
}
}
Take last activity--
public class C extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
}
// close button..set by xml view..you can set it by button listener.
public void close(View v) {
setResult(5);
finish();
}
}
Hopefully, it would solve your problem..cheers!
From documentation for Intent.FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
So to get it work your FinishActivity
must be first one in your Activity stack. In any other cases this solution wouldn't give you anything.
You must perform several steps to perform this:
1) Make FinishActivity
as your launcher activity.
2) Do not provide any view for it and start first Activity
of your application directly from onCreate
callback :
3) Redefine onRestart
callback:
Code sample:
private boolean isNeedToContinue = true;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
}
@Override
protected void onResume() {
super.onResume();
if (isNeedToContinue) {
startActivity(new Intent(this,FirstVisibleActivity.class));
isNeedToContinue = false;
} else {
finish();
}
}
@Override
protected void onRestart() {
super.onRestart();
finish();
isNeedToContinue = false;
}
I guess this is all you need. Good luck!
This worked for me:
Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();