Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:55:59+00:00 2026-06-10T08:55:59+00:00

Anyone knows how to completely end the Activity when going to another Activity ?

  • 0

Anyone knows how to completely end the Activity when going to another Activity? I put a finish() on the activity to terminate it. But when i press the Back Key of the Phone or in the Emulator, it goes back to the last Activity i open. For example:

Activity A is the menu, B is the order, C is also the order, D is the payment.

Activity A going to Activity B, Activity B to Activity C, Activity C to Activity D.
if i press the Back Key of the Phone or the Emulator When i am in the Activity D it goes back to C, B, and A. But the Correct one should be from Activity D going back to Activity A. and if i press the back Key again it will close the App.

Anyone knows how to end Activity clearly?

Thanks!

I have a different program, here’s my game menu.

public class BodyPartsGameActivity extends Activity {

    protected boolean _active = true;
    protected int _splashTime = 1000;
     public void onAttachedToWindow() {
            super.onAttachedToWindow();
            Window window = getWindow();
            window.setFormat(PixelFormat.RGBA_8888);
        }


    MediaPlayer mp1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        StartAnimations();       
    }


        private void StartAnimations() {
            Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
            anim.reset();
            LinearLayout l=(LinearLayout) findViewById(R.id.mainlay);
            l.clearAnimation();
            l.startAnimation(anim);

            anim = AnimationUtils.loadAnimation(this, R.anim.clouds);
            anim.reset();
            Button iv = (Button) findViewById(R.id.cloud);
            iv.clearAnimation();
            iv.startAnimation(anim);





        mp1 = MediaPlayer.create(this,R.raw.officialbackgroundmusic);
        mp1.start();

        final MediaPlayer mp = MediaPlayer.create(this, R.raw.button);
        final Vibrator mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);



        Button newgameButton = (Button)findViewById(R.id.newgame);
        newgameButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent newgameIntent = new Intent(BodyPartsGameActivity.this,pgone.class);
                startActivity(newgameIntent);
                    mp.start();
                    mVibrator.vibrate(500);       
            }
        });

        final MediaPlayer ms = MediaPlayer.create(this, R.raw.button);

        Button highscoresButton = (Button)findViewById(R.id.highscores);
        highscoresButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent highscoresIntent = new Intent(BodyPartsGameActivity.this,highscores.class);
                startActivity(highscoresIntent);
                 ms.start();
                 mVibrator.vibrate(500);
            }
        });


        Button instructionButton = (Button)findViewById(R.id.settings);
       instructionButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent instructionsIntent = new Intent(BodyPartsGameActivity.this,settingstwo.class);
                startActivity(instructionsIntent);
                 mp.start();
                 mVibrator.vibrate(500);
            }
        });


       Button instructionsButton = (Button)findViewById(R.id.instructions);
      instructionsButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent instructionsIntent = new Intent(BodyPartsGameActivity.this,instructions.class);
            startActivity(instructionsIntent);
         ms.start();
        mVibrator.vibrate(500);
        }
       });



      final ImageView mNotification_on_btn=(ImageView)findViewById(R.id.on_btn);
      final ImageView mNotification_off_btn=(ImageView)findViewById(R.id.off_btn);

          mNotification_on_btn.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                      mNotification_on_btn.setVisibility(View.GONE);
                      mNotification_off_btn.setVisibility(View.VISIBLE);
                  }
              });
          ImageView pausresumeButton = (ImageView)findViewById(R.id.on_btn);
         pausresumeButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                    if(mp1.isPlaying())
                    {
                    mp1.pause();
                    }
                    else
                    {
                    mp1.start();
                    }
                            }});


          mNotification_off_btn.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                      mNotification_off_btn.setVisibility(View.GONE);
                      mNotification_on_btn.setVisibility(View.VISIBLE);
                  }
              });


    }

        @Override
        protected void onPause() {
          if (this.isFinishing()){ //basically BACK was pressed from this activity
            mp1.stop();
            Toast.makeText(BodyPartsGameActivity.this, "You Pressed Back Button on Your 'HOME'  The Game was ended.  ", Toast.LENGTH_SHORT).show();
          }
          Context context = getApplicationContext();
          ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
          List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
          if (!taskInfo.isEmpty()) {
            ComponentName topActivity = taskInfo.get(0).topActivity; 
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
              mp1.stop();
              Toast.makeText(BodyPartsGameActivity.this, "YOU LEFT YOUR APP", Toast.LENGTH_SHORT).show();
            }

          }
              super.onPause();
              mp1.reset();
          }

        }

Here’s my Instruction Page:

public class instructions extends Activity{

    MediaPlayer mPlayer;

    protected boolean _active = true;
    protected int _splashTime = 1000;
     public void onAttachedToWindow() {
            super.onAttachedToWindow();
            Window window = getWindow();
            window.setFormat(PixelFormat.RGBA_8888);
        }


    @Override
    public void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.instructions); 
        StartAnimations();

     }


    private void StartAnimations() {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        anim.reset();
        LinearLayout l=(LinearLayout) findViewById(R.id.instbg);
        l.clearAnimation();
        l.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.controll);
        anim.reset();
        Button iv = (Button) findViewById(R.id.hwtoply);
        iv.clearAnimation();
        iv.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.controlll);
        anim.reset();
        Button ib = (Button) findViewById(R.id.cntrls);
        ib.clearAnimation();
        ib.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.controllll);
        anim.reset();
        Button ic = (Button) findViewById(R.id.trivia);
        ic.clearAnimation();
        ic.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.controlllll);
        anim.reset();
        Button iz = (Button) findViewById(R.id.about);
        iz.clearAnimation();
        iz.startAnimation(anim);


        Button back4Button = (Button)findViewById(R.id.back4);
        back4Button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent back4Intent = new Intent(instructions.this,BodyPartsGameActivity.class);
                startActivity(back4Intent);
                finish();

            }
         });


        Button howtoButton = (Button)findViewById(R.id.hwtoply);
        howtoButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent howtoIntent = new Intent(instructions.this,howto.class);
                startActivity(howtoIntent);


            }
        });

        Button ctrlsButton = (Button)findViewById(R.id.cntrls);
        ctrlsButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent ctrlsIntent = new Intent(instructions.this,controls.class);
                startActivity(ctrlsIntent);


            }
        });

        Button triviaButton = (Button)findViewById(R.id.trivia);
        triviaButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent triviaIntent = new Intent(instructions.this,trivia.class);
                startActivity(triviaIntent);


            }
        });

        Button abwtButton = (Button)findViewById(R.id.about);
        abwtButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent abwtIntent = new Intent(instructions.this,about.class);
                startActivity(abwtIntent);


            }
        });




}
    @Override
    protected void onDestroy() {
        setResult(2);
        super.onDestroy();
    }
}

Going back to menu then My game level:

public class gamelevel extends Activity { 
    protected boolean _active = true;
    protected int _splashTime = 1000;
     public void onAttachedToWindow() {
            super.onAttachedToWindow();
            Window window = getWindow();
            window.setFormat(PixelFormat.RGBA_8888);
        }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamelevel); 

        StartAnimations();   
    }
        private void StartAnimations() {
            Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
            anim.reset();
            LinearLayout l=(LinearLayout) findViewById(R.id.gmlvl);
            l.clearAnimation();
            l.startAnimation(anim);

            anim = AnimationUtils.loadAnimation(this, R.anim.controll);
            anim.reset();
            Button iv = (Button) findViewById(R.id.easypg);
            iv.clearAnimation();
            iv.startAnimation(anim);

            anim = AnimationUtils.loadAnimation(this, R.anim.controlll);
            anim.reset();
            Button ib = (Button) findViewById(R.id.mediumpg);
            ib.clearAnimation();
            ib.startAnimation(anim);

            anim = AnimationUtils.loadAnimation(this, R.anim.controllll);
            anim.reset();
            Button ic = (Button) findViewById(R.id.hard);
            ic.clearAnimation();
            ic.startAnimation(anim);


        Button easypButton = (Button)findViewById(R.id.easypg);
        easypButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent easypIntent = new Intent(gamelevel.this,Maingame.class);
                startActivity(easypIntent);
                finish();
            }
        });

        Button mediumButton = (Button)findViewById(R.id.mediumpg);
        mediumButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent mediumIntent = new Intent(gamelevel.this,Mediumgame.class);
                startActivity(mediumIntent);
                finish();
            }
        });

        Button hardButton = (Button)findViewById(R.id.hard);
        hardButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent hardIntent = new Intent(gamelevel.this,Hardgame.class);
                startActivity(hardIntent);
                finish();
            }
        });

        Button back1Button = (Button)findViewById(R.id.back1);
        back1Button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent back1Intent = new Intent(gamelevel.this,BodyPartsGameActivity.class);
                startActivity(back1Intent);
                finish();
            }
        });
    }
        @override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_BACK)
            {
                moveTaskToBack(true);
                return true; // return
            }

            return false;

         }
        @Override
        protected void onDestroy() {

            super.onDestroy();
        }
       }

And here is my easy game:

public class Maingame extends Activity {
    int v,x,y,z;



    public void onAttachedToWindow() {
            super.onAttachedToWindow();
            Window window = getWindow();
            window.setFormat(PixelFormat.RGBA_8888);
             displayIntentData();

////////////////////////////////score calculation/////////////////////
             initControls();
      }
      private void initControls()
      {
          amount1=(EditText)findViewById(R.id.amount1);
          amount2=(EditText)findViewById(R.id.amount2);
          amount3=(EditText)findViewById(R.id.amount3);
          result=(TextView)findViewById(R.id.result);
          calculate=(Button)findViewById(R.id.finish);
          calculate.setOnClickListener(new Button.OnClickListener()
          {public void onClick
          (View  v) { calculate();}});
      }
      private void calculate()
      {

       if(amount1.getText().toString().equals(""))
          {
          x=0;
          }
       else
          {
          x=Integer.parseInt(amount1.getText().toString());
            }

       if(amount2.getText().toString().equals("")) 
          {
          y=0; 
            }
       else
          {
          y=Integer.parseInt(amount2.getText().toString());
            }
       if(amount3.getText().toString().equals(""))
          {
          v=0;
            }
       else
          {
       v=Integer.parseInt(amount3.getText().toString());
            }

          z=x+y+v;
          result.setText(Integer.toString(z));


      ////////////////////////////////score calculation/////////////////////

                findViewById(R.id.finish).setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        Intent intent = new Intent(Maingame.this,score.class);
                        intent.putExtra("key",((TextView)findViewById(R.id.result)).getText().toString());
                        startActivity(intent);
                    }
                });
            }
            @Override
            protected void onNewIntent(Intent intent) {
                super.onNewIntent(intent);
                setIntent(intent);
                displayIntentData();
            }

            private void displayIntentData(){
                Intent intent = getIntent();
                TextView tv = (TextView)findViewById(R.id.intentData);
                Bundle extras=intent.getExtras();


                if(extras!=null){
                    tv.setText(" "+extras.getString("key"));
                }else{
                    tv.setText("");
                }
            }



    private Gallery gallery;

    TextView select1;
    TextView select2;
    TextView select3;

    TextView result;

     EditText amount1;
     EditText amount3;
     EditText amount2;
     EditText pauseb;


     Button calculate;

    @Override
    public void onCreate(Bundle savedInstanceState) {


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


     Button pauseButton = (Button)findViewById(R.id.pause);
     pauseButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Intent backcreIntent = new Intent(Maingame.this,pauseg.class);
            startActivity(backcreIntent);

        }
       });

    gallery = (Gallery) findViewById(R.id.examplegallery);
    gallery.setAdapter(new AddImgAdp(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView parent, View v, int position, long id) {

        if (position == 0 ) {
            //Toast.makeText(Maingame.this, "heart", Toast.LENGTH_SHORT).show();
             select1 = (TextView) findViewById(R.id.txt1);
              select1.setText("Head");

            amount1.setText("1000");

        } else if (position == 1) {

            select1 = (TextView) findViewById(R.id.txt1);
              select1.setText("Eye");

              amount1.setText("0");

        } else if (position == 2) {

            select1 = (TextView) findViewById(R.id.txt1);
              select1.setText("Intestine");

              amount1.setText("0");

        } else if (position == 3) {

            select2 = (TextView) findViewById(R.id.txt2);
              select2.setText("Ear");

              amount2.setText("0");

        } else if (position == 4) {

            select2 = (TextView) findViewById(R.id.txt2);
              select2.setText("liver");

              amount2.setText("0");

        } else if (position == 5) {

            select2 = (TextView) findViewById(R.id.txt2);
              select2.setText("Arm");

              amount2.setText("1000");

        } else if (position == 6) {

            select3 = (TextView) findViewById(R.id.txt3);
              select3.setText("Hand");

              amount3.setText("0");
              Toast.makeText(Maingame.this, "Click Finish To Submit Your Answer", Toast.LENGTH_LONG).show();

        } else if (position == 7) {

            select3 = (TextView) findViewById(R.id.txt3);
              select3.setText("Foot");

              amount3.setText("0");
              Toast.makeText(Maingame.this, "Click Finish To Submit Your Answer", Toast.LENGTH_LONG).show();

        } else if (position == 8) {

            select3 = (TextView) findViewById(R.id.txt3);
              select3.setText("Legs");

              amount3.setText("1000");
              Toast.makeText(Maingame.this, "Click Finish To Submit Your Answer", Toast.LENGTH_LONG).show();

        }
    }
    });


    Button clear = (Button)findViewById(R.id.clear);
    clear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ((TextView) findViewById(R.id.txt1)).setText("");
            ((TextView) findViewById(R.id.txt2)).setText("");
            ((TextView) findViewById(R.id.txt3)).setText("");
        }
    });

    }


    public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    // Adding images.
    private Integer[] Imgid = {
    R.drawable.head,R.drawable.eye, R.drawable.largeintestine,R.drawable.ear, R.drawable.liver, R.drawable.arm
    ,R.drawable.hand,R.drawable.foot,R.drawable.legs};

    public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.BodyPartsGame);
    GalItemBg = typArray.getResourceId(R.styleable.BodyPartsGame_android_galleryItemBackground, 0);
    typArray.recycle();
    }

    public int getCount() {
    return Imgid.length;
    }

    public Object getItem(int position) {
    return position;
    }

    public long getItemId(int position) {
    return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imgView = new ImageView(cont);

    imgView.setImageResource(Imgid[position]);
    // Fixing width & height for image to display
    imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
    }
    }
    }

When i press the back button of the Phone or emulator, it goes back to the last activity i open. if i open game menu —–> instruction —–> back to menu —–> new game —-> game level —–> easy game and i press back button it goes back to my recent open Activity.

How to fix that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:56:01+00:00Added an answer on June 10, 2026 at 8:56 am

    you don’t need to explicitly call the finish every time you start the next activity, just set android:noHistory=”true” to those activities that you want to remove from activity stack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how I can create a Completely transparent image or given am
Anyone knows why my sql query doesn't work after using a try catch function?
Anyone knows the equivalent of a label control in an android device? Text box
Anyone knows a good way to use http live streaming tools on non-Mac platforms?
Anyone knows how to perform such query in Postgresql? SELECT * FROM tabA WHERE
Anyone knows a simple JavaScript library implementing the UNZIP algorithm? No disk-file access, only
Anyone knows of some good Unicode tutorials with examples in C? I have to
Anyone knows the difference between System.Diagnostic.Trace and System.Diagnostic.TraceSource classes? I've been using Trace for
anyone knows any java utility method that would convert windows paths to DOS 8.3
I'm playing with the idea of making a completely JavaScript-based zip/unzip utility that anyone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.