Question

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?

Was it helpful?

Solution

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();
    }
}

OTHER TIPS

If you do not want the back button to go to the current activity on back press finish the activity.

If you have a dialog opened, overide the onPause method of the activity and close the dialog. onPause will be called when the activity goes offscreen.

We only overide the onBackPressed method when we need to do something specific otherwise in normal cases we just leave it.

If you want to open another activity and want to finish the previous activity then use the finish(); function after calling the intent of another activity.

it will finish the current activity and open the new activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top