Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually pause Activity in Android

I have two Activities , A and B. I called B from A throught this code :

 Intent myIntent = new Intent(this, myAcitivity.class);        
 myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(myIntent);

and on B , I placed a button to go back to activity A by pausing activity B. I tried to pause B so that it goes to background and go to A , but it is working. I tried

One Solution :

moveTaskToBack(true);

Instead of placing B in background , it is also placing A in background.

Any solutions ?

like image 204
Hammad Shahid Avatar asked Jan 12 '23 20:01

Hammad Shahid


2 Answers

To override the behavior of Back Button you can override onBackPressed() method in your Activity which is called when you press the back button:

@Override
public void onBackPressed() {
   moveTaskToBack(true);  // "Hide" your current Activity
}

By using moveTaskToBack(true) your Activity is sent to background but there is no guarantee it will remain in the "pause" state, Android can kill it if it needs memory. I don't know why you want this behavior I think it would be better to save Activity state and recover it when you are back or simply, launch another Intent with the new Activityyou want to bring.

Or,

Use this code onBackPressed()

boolean mIsPaused = false;

final Thread workerThread = new Thread(new Runnable() {
  @Override
  public void run() {
  doA();
  checkPause();
  doB();
  checkPause();
  ...
 }
 }
});

private void checkPause() {
while(isPaused()) {
  // you could also use the notify/wait pattern but that is probably needless     complexity for this use case.
  Thread.sleep(50);
 }
}

private synchronized boolean isPaused() {
return mIsPaused;
}

 private synchronized void setPaused(boolean isPaused) {
 mIsPaused = isPaused;
 }


 pauseButton.setOnLongClickListener(new View.OnLongClickListener() {
 @Override
 public boolean onLongClick(View v) {
  // disable any UI elements that need it
  setIsPaused(true);
 }
});

 unPauseButton.setOnLongClickListener(new View.OnLongClickListener() {
  @Override
 public boolean onLongClick(View v) {
  // re-enable any UI elements that need it
  setIsPaused(false);
 }
});
like image 105
Mitesh Shah Avatar answered Jan 18 '23 16:01

Mitesh Shah


Android is already doing this for you. Say you are in activity A. You start activity B with:

Intent myIntent = new Intent(this, myAcitivity.class);
startActivity(myIntent);

onPause() for current activity will be called before you go to myActivity, where onCreate() gets called. Now if you press back button, myActivity's onPause() gets called, and you move back to activity A, where onResume() is called. Please read about activity life cycle in the docs here and here.

To save the state of an activity, you must override onSaveInstanceState() callback method:

The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate() methods.

Example:

static final String STATE_SCORE = "playerScore";
static final String STATE_LEVEL = "playerLevel";

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);
    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

And when your activity is recreated, you can recover your state from the Bundle:

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
}

There's more on this in the docs, please have a good read about saving/restoring your activity state here.

like image 39
Melquiades Avatar answered Jan 18 '23 15:01

Melquiades