Intent.FLAG_ACTIVITY_CLEAR_TOP doesn't deletes

2019-01-25 05:16发布

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 android does that automatically from this Is quitting an application frowned upon?. but yet i want to close my application.

So what i am doing to close application is i am using Intent.FLAG_ACTIVITY_CLEAR_TOP flag to delete the activity stack.

Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

And in onCreate of FinishActivity.class i am calling this.finish() but application is not get closed and previous activity gets reopened.

FinishActivity.class

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

Update :

Here is the scenario

MainActivity->Activity2->Activity3->FinishActivity

Here Activity2 is gets opened after finishing the activity.

How do i achieve this? Any idea and suggestion will be appreciated.

Thanks & Regards

10条回答
做个烂人
2楼-- · 2019-01-25 06:09

This finally worked for me 100% of the time. I tried all of the flags standalone, never worked for all instances of the code. Used all the flags everywhere I want this functionality, about 5 different places, and it now works.

            Intent intent = new Intent(actvSignIn.this, actvNearbyPlaces.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
查看更多
爷的心禁止访问
3楼-- · 2019-01-25 06:12

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();
查看更多
小情绪 Triste *
4楼-- · 2019-01-25 06:18

As other answers have the following should work.

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_CLEAR_TASK);

For API level < 11, Intent.FLAG_ACTIVITY_CLEAR_TASK is not available. Instead I used IntentCompat.FLAG_ACTIVITY_CLEAR_TASK from support library.

See IntentCompat

Might help someone stumbling across.

查看更多
beautiful°
5楼-- · 2019-01-25 06:22

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!

查看更多
登录 后发表回答