Alex Rivera | Logout

Android finish Activity and start another one

Asked 2011-09-29T15:47:42.353
27

I'm curious about one thing. How can I finish my current activity and start another one.

Example :

MainActivity
    --(starts)--> LoginActivity
        --(if success, starts)--> SyncActivity
            --(if success start)--> MainActivity (with updated data).

So I want when SyncActivity starts MainActivity after succesfull sync and if I press back button not to return to SyncActivity or any other activity opened before SynActivity.

I've tried with this code :

Intent intent = new Intent(Synchronization.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
this.finish();

but it's not working properly. Any ideas how to get the things to work properly?

Edit
Report

1 Answer

59

Use

Intent intent = new Intent(SyncActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
answered 2011-09-29T16:33:57.200

Your Answer