0

Hello how do you finish Main activity Assume that there are 3 Activities and 1 Fragment

LoginActivity , MainActivity, infoFrgMent, ChangePwdActivity.

The scenario is when I loggedin in LoginActivitythen MainActivity will show up LoginActivity will finish() then i will go to my info which is 'infoFrgMent' then i want to change my password after i changed my password.

LoginActivity will shows up Again to relogin but whenever i try to press back MainActivity shows up and didn't finished.

Neil
  • 67
  • 6

3 Answers3

2

You need to remove previous activities form stack

setFlags to intent from MainActivity to LoginActivity

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

refer : https://developer.android.com/guide/components/activities/tasks-and-back-stack.html

Balu Sangem
  • 712
  • 4
  • 16
0

What you need is to add the Intent.FLAG_CLEAR_TOP. This flag makes sure that all activities above the targeted activity in the stack are finished and that one is shown.

Another thing that you need is the SINGLE_TOP flag. With this one you prevent Android from creating a new activity if there is one already created in the stack.

Just be wary that if the activity was already created, the intent with these flags will be delivered in the method called onNewIntent(intent) (you need to overload it to handle it) in the target activity.

Then in onNewIntent you have a method called restart or something that will call finish() and launch a new intent toward itself, or have a repopulate() method that will set the new data. I prefer the second approach, it is less expensive and you can always extract the onCreate logic into a separate method that you can call for populate.

Attiq ur Rehman
  • 475
  • 1
  • 6
  • 21
-1

To finish another Activity you have to create a static method to finish this Like here:

MainActivity.java

private static MainActivity activity;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    activity = this;

}

public static void finishThis()
{
    try
    {
        if (activity != null)
        {
            activity.finish();
        }
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}

And call it like this:

AnotherActivity.java

MainActivity.finishThis();

That's it

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • Worked for me Thank you! – Neil Feb 06 '18 at 07:47
  • 2
    Placing Activities (a child-class of Context) in a static context is a horrible idea. It can cause memory leaks. See: https://stackoverflow.com/questions/37709918/warning-do-not-place-android-context-classes-in-static-fields-this-is-a-memory – Zoe Feb 06 '18 at 07:48
  • This is very bad practise. static context leads to memory leaks. – Balu Sangem Feb 06 '18 at 08:27
  • @BaluSangem & ZoeI know but it can cause memory leaks but whats the solution... It will be better if you provide a perfect solution rather than a down-vote. – Vishal Chhodwani Feb 06 '18 at 08:58
  • @VishalChhodwani OOPS . Down vote is not from me. Please see my solution below – Balu Sangem Feb 06 '18 at 09:00
  • No Issue, your answer has my comment, have a look. – Vishal Chhodwani Feb 06 '18 at 09:01
  • @vishal there is a post with a good answer without memory leaks. If you know it causes memory leaks, why did you suggest it in the first place? – Zoe Feb 06 '18 at 10:31