Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onBackPressed to hide Not destroy activity

Tags:

android

i know how to cancel back keypress, so that the activity / main window stays visible:

public void onBackPressed() {     return;              } 

my aim is to hide the activity, however, without finishing it, how do you do that in the onBackPressed event?

i.e. I would like to get as far as onPause(), but not evoke the onBackPressed() default behaviour that essentially calls finish(). another way to put it is that i would like to mimic onUserLeaveHint() ?

any help appreciated!

like image 526
Cel Avatar asked May 06 '11 16:05

Cel


People also ask

Does onBackPressed destroy activity?

But the proper answer is - Don't do it. the Android OS will automatically free up memory when it needs memory. By not freeing up memory, your app will start up faster if the user gets back to it. Please see here for a great write-up on the topic.

Can the system destroy an activity without calling onDestroy?

You don't need to call stop( ) method. Android system automatically go thru those life cycle methods. But apparently onDestroy() always called after onStop() . If you want to kill activity just call finish() , it will destroy your activity.

How do you implement back pressed activity?

Step 3: Working with MainActivity.java file In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so. Otherwise, don't exit.


2 Answers

If you want to mimic the "Home" button on specific Activities just do this:

Below API 5:

@Override public boolean onKeyDown(int keyCode, KeyEvent event) {     if (keyCode == KeyEvent.KEYCODE_BACK) {         moveTaskToBack(true);         return true;     }     return super.onKeyDown(keyCode, event); } 

Above and on API 5:

@Override public void onBackPressed() {   moveTaskToBack(true); } 

It will move the Task to background.. when you return, it will be as it was.

You can see more information about this here: Override back button to act like home button

like image 110
neteinstein Avatar answered Sep 20 '22 11:09

neteinstein


If you want to mimic the "Home" button on specific Activities :

1st Method :

@Override public void onBackPressed() {    Log.d("CDA", "onBackPressed Called");    Intent setIntent = new Intent(Intent.ACTION_MAIN);    setIntent.addCategory(Intent.CATEGORY_HOME);    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    startActivity(setIntent); } 

2nd Method :

@Override public void onBackPressed() {    moveTaskToBack(true); } 

And if you want to move to the previous activity without destroying current :

@Override public void onBackPressed() {     startActivity(new Intent(CurrentActivity.this, DestinationActivity.class); } 

And now from any activity if you want to open the activity which is in Background . I name CurrentActivity. you could call it form anywhere..like..it wil take that actvity and put it to top of stack . and open it where you left off.

Intent intent = new Intent(FromAnyActivity.this, CurrentActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent); 

Flags :

FLAG_ACTIVITY_REORDER_TO_FRONT : To reorder the activity from stack

FLAG_ACTIVITY_CLEAR_TOP : To remove all the activities from the top

like image 34
Zar E Ahmer Avatar answered Sep 20 '22 11:09

Zar E Ahmer