i have a splash screen that has some code that i only want called once so after showing the splash screen it goes to my main game, and i don't want it going back to the splash screen again until the game is launched again. but when i hit back on my main screen, it goes back to the splash screen. how do i make the game close when the back button is pressed from the main screen instead of going back to the splash screen?
here is what i have right now on my main game, but it still goes back to the splash...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Toast.makeText(getApplicationContext(),"back was pressed",Toast.LENGTH_LONG).show();
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Call finish() in the splash activity right after you do startActivity() :)
If you are starting the Activity you can use FLAG_ACTIVITY_NO_HISTORY. See this for a reference. I'm sure you can do the same somehow if it is your initial activity.
You could put a call to finish() in your onResume method for the Splash activity. Jack's solution is probably best though.
Related
I am developing an application in API level 2.2. In my application whenever I press Home button it forces my app to exit, and when I relaunch that app by clicking on its icon, it starts from where it exited. I have tried to change/ override functionality of Home button but it does not works.
I have tried as
`
public boolean onKeyDown (int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
startActivity(new Intent(CategoryFirstActivity.this,LastActivity.class));
finish();
}
return false;
}`
But it works for Back button only and not for Home button.
I have also tried to get keyCode of Home key but I didn't get it.
I need solution to change Home Button functionality.
Simply put: better you do not change the behavior of your home button...
Instead of trying to reprogram your Home button, you could try using some of the Activity callbacks that Android provides. You can take a look at the Activity lifecycle here:
http://developer.android.com/reference/android/app/Activity.html
For example, you could try overriding the onPause callback in an effort to achieve the effect you're going for.
#Override
public void onPause() { /* do stuff */ }
In your case, do stuff might be returning the application to its initial state or forcing it to exit. I've never attempted a force quit myself, but there are some answers on this StackOverflow post:
How to exit from the application and show the home screen?
Best of luck!
I need for an activity to return to the previous activity but if click again on that button it resumes the activity where it was for the last time.
This is the desired process:
I click on a button and the activity starts. If I click on the Back button to return to the previous activity, but if the user clicks again the button the activity resumes to where it was for the last time.
I have tried with onBackPressed(){moveTaskToBack(true)}; but it just minimizes the whole application. Any suggestions?
Edit:Hi, sorry for the explanation. I mean that if in an activity the user selects back button, the activity is put in background and the previous ( not finished) activity is loaded. Then if I click on a specific button again the activity in background is resumed.
Thanks
This code worked for me. Hope it works for you too:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
This is be cause all your activities are part of the same task. When calling moveTaskToBack(true), it will move the entire application to the back if they are all part of the same task.
To fix this, you would need to start the activity to be minimized in a separate task.
When calling my PreferenceActivity from a screen, and after pressing the Back button, the return screen is always the main screen (the activity that shows after app start) of the application. Is it possible to go back to the previous screen?
I tried to solve it through overriding the onKeyDown (inside my PreferenceActivity class) method without luck:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
Thanks in advance for any help.
Assuming you use Activities/Intents as designed, there's actually no special code required. Pushing back will automatically stop the current activity, and go back to the activity which called it. See the Android Activity and Task guide.
we used back key by calling
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
but,we used 4 classes in our application.if we gives back button,its working well and returning to application home page.but wen we go next time its in same class.(we used above back key coding in 3 rd class,its remains same page).
is there any alternative method.if anybody knows pls reply.
moveTaskToBack() only moves the Activity to the back of the activity stack, it doesn't actually close the Activity. What you want is finish(), which will close the Activity and Android will automatically take you back to the previous Activity in the stack.
For more information, see the documentation: http://developer.android.com/guide/topics/fundamentals.html#acttask
Try overriding onBackPressed()
use this...
onBackPressed not close Current Activity In Android
I having problem in my application.. I am pressing back button in my third activity but its running in running in background(i noticed by used Log statement). If again load application its starts from first activity instead of third activity. please suggest some idea to do this..
This is my back button coding
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
moveTaskToBack(true);
}
return true;
}
Thanks in advance..
The back button pauses your current activity and pop it from the activity stack, so the previous activity will be shown. You don't have and in most cases you just shouldn't override this behaviour.
Basicly, if you're in your third activity and you press the home button, then relaunch the application the third activtiy will be shown.
Please, refer to Activity lifecycle.
When you are in your third activity and then press home button. The next time if you want your activity to start from the third activity, Long press the home button and then invoke the application from this list rather than launching it from launcher activity.