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.
Related
I am stuck in the using the Flag activity clear top usage. It creates a huge problem to me. Sometimes it works and sometimes not. I dont wtf matter is. It is really confusing me. Please help
Here I go from Login Acitivy -> Activity A -> Activity B->InviteGroupViaPinEmailActivity-> on the Button click, I am opening a Alert Dialog and on the press of Exit button of this dialog,I am calling the following code.
Intent userMenu = new Intent(InviteGroupViaPinEmailActivity.this,LoginActivity.class);
userMenu.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
userMenu.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(userMenu);
finish();
For a while it comes on LoginActivity, But when I press back all the activities Activity B->Acitivity A again comes on, and then it is finally exited.
Whats the problem .Please help me.
I think you finish your Login Acitivity while you go to the LoginActivity -> Activity A. When you are using clear top flag then your Login Activity should be alive in your stack. Please make sure that you are not finish your login activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.e("onkeyDown>>>>", "Called>>>>>");
finish();
}
return super.onKeyDown(keyCode, event);
}
You can use
android:noHistory="true" in manifest for activities to rectify all problems.Whether you tried?
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.
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.