how to close main activity after closing another activity in android?

Sandunka Mihiran

I am developing an android application. So in there I have button called "Aboutus". when I click that one it starts another activity and show corresponding view.

Here is the code for button Aboutus click event.

    aboutus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
             Intent intent = new Intent(MainActivity.this,AboutUs.class);
             startActivity(intent);
        }

    });

}

And then in that aboutus activity I have back button.when we press that back button it will go to the main activity again. back button key event goes like this..

    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(AboutUs.this,MainActivity.class);
            intent.putExtra("aboutus", true);
            startActivity(intent);
            finish();
            System.exit(0);
        }

    });

here i put some intent extras for some other purposes.In this case they do not matter. So and then again in mainactivity i have exit button. which should kill whole app.

and my exit button code goes like this.

exit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
              finish();
              System.exit(0);
        }

    });

Exit button works perfectly (it ends the app) except for one scenario. if we click on aboutus and go to that activity and then press back button on that aboutus activity and then again come back to main activity and finally when i want to exit it won't kill whole app, instead it again goes to the about activity.

in conclusion, MainActivity---> click aboutus button(no problem in here.this will start aboutus activity)

Aboutus----->click back button(this also works fine. go back to main activity)

MainActivity--> Exit button (not working .it goes to aboutus activity again)

So how to fix this problem?

Krupal Shah

use intent flag ACTIVITY_CLEAR_TOP to clear all activities on top of it:

            Intent intent = new Intent(AboutUs.this,MainActivity.class);
            intent.putExtra("aboutus", true);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            startActivity(intent);

FLAG_ACTIVITY_CLEAR_TOP From the doc:

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android: how to add another fragment to the main activity

From Dev

How to close one activity to another

From Dev

How to launch main activity of one application from another application on Android

From Dev

how back to the main activity in android?

From Dev

Open Info Activity without closing Main Activity

From Dev

Android Close Activity After All Dialogs Are Dismissed

From Dev

how to go back to previous activity with closing current activity in android?

From Dev

Android Another Activity call Main Activity Databasehelper Function fail

From Dev

Calling another class in the Main activity class in android

From Dev

Calling another class in the Main activity class in android

From Dev

How to close previous activity after the notification is clicked and opens a new activity

From Dev

NullPointerException whn closing an Activity from another Activity

From Dev

How can Main Activity call another Activity A and send its context to it?

From Dev

How to Make another Activity as Main Activity(start up)

From Dev

How to finish all Activities except Main Activity and call another Activity

From Dev

How to access a variable declared in the main activity in another activity

From Dev

How to start an another activity in android after webview has been loaded?

From Dev

How to start an another activity in android after webview has been loaded?

From Dev

app closes after closing an activity

From Dev

android - How to close an activity on button click?

From Dev

Close activity from another activity through an intent

From Dev

Close activity from another activity through an intent

From Dev

How to exit from Android App even if current Activity is not Main Activity

From Dev

How does manifest recognize the particular activity as a main or first activity in android?

From Dev

How do I refresh TextView on activity calling after closing alert dialog in Android?

From Dev

Android: Redirect to another Activity after delay

From Dev

Start same android last activity on relaunch after closing the app

From Dev

Closing an Android application after starting it with FLAG_ACTIVITY_NEW_TASK

From Dev

Android Activity's - How can I open another activity in android?

Related Related

  1. 1

    Android: how to add another fragment to the main activity

  2. 2

    How to close one activity to another

  3. 3

    How to launch main activity of one application from another application on Android

  4. 4

    how back to the main activity in android?

  5. 5

    Open Info Activity without closing Main Activity

  6. 6

    Android Close Activity After All Dialogs Are Dismissed

  7. 7

    how to go back to previous activity with closing current activity in android?

  8. 8

    Android Another Activity call Main Activity Databasehelper Function fail

  9. 9

    Calling another class in the Main activity class in android

  10. 10

    Calling another class in the Main activity class in android

  11. 11

    How to close previous activity after the notification is clicked and opens a new activity

  12. 12

    NullPointerException whn closing an Activity from another Activity

  13. 13

    How can Main Activity call another Activity A and send its context to it?

  14. 14

    How to Make another Activity as Main Activity(start up)

  15. 15

    How to finish all Activities except Main Activity and call another Activity

  16. 16

    How to access a variable declared in the main activity in another activity

  17. 17

    How to start an another activity in android after webview has been loaded?

  18. 18

    How to start an another activity in android after webview has been loaded?

  19. 19

    app closes after closing an activity

  20. 20

    android - How to close an activity on button click?

  21. 21

    Close activity from another activity through an intent

  22. 22

    Close activity from another activity through an intent

  23. 23

    How to exit from Android App even if current Activity is not Main Activity

  24. 24

    How does manifest recognize the particular activity as a main or first activity in android?

  25. 25

    How do I refresh TextView on activity calling after closing alert dialog in Android?

  26. 26

    Android: Redirect to another Activity after delay

  27. 27

    Start same android last activity on relaunch after closing the app

  28. 28

    Closing an Android application after starting it with FLAG_ACTIVITY_NEW_TASK

  29. 29

    Android Activity's - How can I open another activity in android?

HotTag

Archive