-4

Login_page>>home_page>>search_account>>delete_account

Then I delete an account and I want go to login_page. I mean what is actual code for close all activity without login_page?

This is my code, but is not work.

AlertDialog.Builder builder = new AlertDialog.Builder(delete_class.this);
    builder.setTitle("Delete");
    builder.setMessage("Do you want to delete your account ??");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String id = MainActivity.return_id();
            Firebase firebase = new Firebase("user");
            firebase.child(id).removeValue();
            finish();
            Toast.makeText(delete_class.this,"Account deleted successfully",Toast.LENGTH_LONG).show();
        }
    });
    builder.setNegativeButton("No",null);
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
JRG
  • 4,037
  • 3
  • 23
  • 34
Atik Faysal
  • 158
  • 1
  • 13

6 Answers6

2

Use this method to open login activity it will kill all of other activity in back stack

public static void GoToActivityAsNewTask(Activity context, Class<?> clazz) {
        Intent intent = new Intent(context, clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
        context.finish();

    }

call will be like

GoToActivityAsNewTask(this, LoginActivity.class);

Hope this will solve your issue

rana_sadam
  • 1,216
  • 10
  • 18
1

Use the CLEAR_TOP flag in intent. It will resolve your issue.

Intent intent = new Intent(this, LoginActivity.class);   
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);
piet.t
  • 11,718
  • 21
  • 43
  • 52
codecracker93
  • 74
  • 1
  • 6
0

You simply need to start the login activity using the CLEAR_TOP flag.

Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

By doing this, all the previous Activities will be closed and only the LoginActivity will be on the stack.

rontho
  • 439
  • 4
  • 11
0

You can try this one.

// Add activity
public static void addActivities(String actName, Activity _activity) {
    if (Config.screenStack == null)
        Config.screenStack = new HashMap<String, Activity>();
    if (_activity != null)
        Config.screenStack.put(actName, _activity);
}

// Remove Activity
public static void removeActivity(String key) {
    if (Config.screenStack != null && Config.screenStack.size() > 0) {
        Activity _activity = Config.screenStack.get(key);
        if (_activity != null) {
            Config.screenStack.remove(key);
            _activity.finish();
        }
    }
}

// Close all activities or screens
public static void closeAllScreens()
{
   if (Config.screenStack != null && Config.screenStack.size() > 0)
   {
       for (int i = 0; i < Config.screenStack.size(); i++)
       {
          Activity activity = Config.screenStack.get(i);
          if (activity != null)
          {
            Config.screenStack.remove(key);
            activity.finish();
          }
       }
    }
}

User add activities before setContentView to add into the stack.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addActivities("DemoActivity", DemoActivity.this)
    setContentView(R.layout.activity_create_feed_post);
}

For more info you can refer this.

Andy Developer
  • 3,071
  • 1
  • 19
  • 39
0

Use this method :

    public static void finishAll(Context context)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        ((Activity) context).finishAffinity();
    }
    else
    {
        ((Activity) context).finish();
                }
}

Hope it helps !

Gagan
  • 745
  • 10
  • 31
0

You should probably use activity.startActivityForResult(). Make sure to check the Android docs regarding this topic.

ByteWelder
  • 5,464
  • 1
  • 38
  • 45
  • `startActivityForResult` is for starting an `Activity` and getting result from it. Since he wants to start a new LoginActivity and clear all previous Activity stack he can't get any result. – Yasin Kaçmaz Jul 27 '17 at 09:28
  • If each Activity in the chain returns a result when it's done with a certain action, that way the whole chain can be cleaned up. So on activity result, you check if the operation of the next activity was finished (e.g. delete something) and then you know you can close your current Activity. – ByteWelder Jul 28 '17 at 13:03