如何在 Android 中手动暂停 Activity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20185989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:26:17  来源:igfitidea点击:

How to manually pause Activity in Android

androidandroid-intent

提问by Hammad Shahid

I have two Activities , Aand B. I called Bfrom Athrought this code :

我有两个活动,AB。我BA这段代码中调用:

 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 Aby pausing activity B. I tried to pause Bso that it goes to background and go to A, but it is working. I tried

然后B,我放置了一个按钮,A通过暂停活动返回活动B。我试图暂停,B以便它转到后台并转到A,但它正在工作。我试过

One Solution :

一种解决方案:

moveTaskToBack(true);

moveTaskToBack(true);

Instead of placing Bin background , it is also placing Ain background.

它不是放置B在背景中,而是放置A在背景中。

Any solutions ?

任何解决方案?

回答by Mitesh Shah

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

要覆盖后退按钮的行为,您可以覆盖按下后退按钮时调用的onBackPressed()方法Activity

@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.

通过使用moveTaskToBack(true)您的 Activity 被发送到后台,但不能保证它会保持在“暂停”状态,如果它需要内存,Android 可以杀死它。我不知道你为什么想要这种行为,我认为最好保存 Activity 状态并在你回来时恢复它,或者干脆用你想要带来的新 Activity 启动另一个 Intent。

Or,

或者,

Use this code onBackPressed()

使用此代码 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);
 }
});

回答by Melquiades

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

Android 已经在为你做这件事。假设您在活动 A 中。您开始活动 B:

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 hereand here.

在您转到 myActivity 之前,将调用当前活动的 onPause(),其中 onCreate() 被调用。现在,如果您按下后退按钮,myActivity 的 onPause() 将被调用,然后您将返回到活动 A,其中 onResume() 被调用。请在此处此处的文档中阅读有关活动生命周期的信息

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

要保存活动的状态,您必须覆盖 onSaveInstanceState() 回调方法:

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.

当用户离开您的 Activity 时,系统会调用此方法并将 Bundle 对象传递给它,该对象将在您的 Activity 意外销毁时保存。如果系统稍后必须重新创建活动实例,它会将相同的 Bundle 对象传递给 onRestoreInstanceState() 和 onCreate() 方法。

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:

当您的 Activity 重新创建时,您可以从 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.

文档中有更多关于此的内容,请仔细阅读有关在此处保存/恢复活动状态的信息