Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I always finish one activity before going to another?

Do you always call finish() on some activity before going to another activity?

For example, in order to prevent user going to the previous activity via mobile back button, some people suggest that you should finish all activities, except the main one. This way, the back button always returns you to the main activity (or any other activity you think a user should be navigated). This is done by overriding back button behaviour.

Bad thing of this is when there is a dialog run from the Handler which try to runs after the activity finished (http://dimitar.me/android-displaying-dialogs-from-background-threads/).

What is your rule of thumb on this issue? Call finish() in some smarter way or overriding back button to direct user to page of your choice?

like image 877
sandalone Avatar asked Nov 02 '11 08:11

sandalone


People also ask

How do you finish multiple activities?

When the user is on Activity D and click a button called exit, the application should go back to Activity B and finish the Activities C and D.

How do you finish an activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do I end an activity in another class?

Say finish(); Like this you can call Activity's finish method from another class. It is perfectly working.


Video Answer


1 Answers

If you understood the workflow of an Android application, there should be no need to override the back-button (except for some special cases like Games for example).

If you don't want the user to get back to the previous Activity, finish it. There should be no need to override the back-button for that.

public class Activity1 extends Activity{

    // Some onclick-Handler
    public void onButtonClick(View v){
        Intent i = new Intent(this, Activity2.class);
        this.startActivity(i);
        // Don't want you to return:
        this.finish();
    }
}
like image 175
Lukas Knuth Avatar answered Sep 27 '22 23:09

Lukas Knuth