Return to the main screen after the operation is completed.

I have an Activity that continues to sound until the user clicks a button. When a button is pressed, a message is displayed. After the message is displayed, instead of the user pressing the "Back" button to exit, I want my activity to go to the main screen on my own.

Does the method use finish()this? If so, how and where to implement it?

Any help would be greatly appreciated. Thanks

+5
source share
5 answers
    Intent i= new Intent("package.homescreenactivity");//homescreen of your app.
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(i);
    finish(); 

Return to the main screen, clearing the action stack.

, . http://developer.android.com/guide/components/tasks-and-back-stack.html. , back back stack.

: (15 2014 )

Backstack . , .

"" .

http://developer.android.com/design/patterns/navigation.html

+7

finish() . .

+1

1:

public void onClick(View v) {
    // Show message here
    moveTaskToBack(true);
    }

case 2:

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            alertbox.setTitle(res.getString("Title"));
            alertbox.setMessage(res.getString("Exit"));
            alertbox.setIcon(R.drawable.logo);
            alertbox.setPositiveButton(res.getString(R.string.Yes),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                            exit();
                        }
                    });

            alertbox.setNeutralButton(res.getString(R.string.No),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                        }
                    });

            alertbox.show();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        moveTaskToBack(true);

    }
+1

Assuming you show the message as alertdialog, call finish()when you click alertdialog.

If you use the Toast()call button finish()on the button, click after Toast().

+1
source

you can apply the method finish()on the button onClick()and after the call startActivity()you can apply the completion method as shown below.

startActivity(intent obj);
finish(); 
0
source

All Articles