Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to clearly end the activity when going to another activity
    primarykey
    data
    text
    <p>Anyone knows how to completely end the <code>Activity</code> when going to another <code>Activity</code>? I put a <code>finish()</code> 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 <code>Activity</code> i open. For example: </p> <p>Activity A is the menu, B is the order, C is also the order, D is the payment.</p> <p><code>Activity A</code> going to <code>Activity B</code>, <code>Activity B</code> to <code>Activity C</code>, <code>Activity C</code> to <code>Activity D</code>. if i press the Back Key of the Phone or the Emulator When i am in the <code>Activity D</code> it goes back to C, B, and A. But the Correct one should be from <code>Activity D</code> going back to <code>Activity A</code>. and if i press the back Key again it will close the App.</p> <p>Anyone knows how to end Activity clearly?</p> <p>Thanks!</p> <p>I have a different program, here's my game menu.</p> <pre><code>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&lt;RunningTaskInfo&gt; 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(); } } </code></pre> <p>Here's my Instruction Page:</p> <pre><code>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(); } } </code></pre> <p>Going back to menu then My game level:</p> <pre><code>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(); } } </code></pre> <p>And here is my easy game:</p> <pre><code>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 &amp; height for image to display imgView.setLayoutParams(new Gallery.LayoutParams(80, 70)); imgView.setScaleType(ImageView.ScaleType.FIT_XY); imgView.setBackgroundResource(GalItemBg); return imgView; } } } </code></pre> <p>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.</p> <p>How to fix that?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload