Android : 어떤 상황에서 대화 상자가 나타나 onPause ()가 호출됩니까?
Android 활동 문서 의 스 니펫 ( " 전경 수명 "행으로 스크롤 )은 다음과 같이 말합니다.
활동은 자주 포 그라운드 안팎으로 전환 될 수 있습니다. 예를 들어
onPause()
기기가 절전 모드로 전환 되거나 대화 상자가 표시 될 때 호출 됩니다 .
나는 이것을 잘 이해하지 못한다. 어떤 상황에서 이런 일이 발생해야합니까? 되어 onPause()
문제의 대화의 맥락이 대화 상자가 표시되어야하는 상단의 활동과 다른 경우에만 호출?
편집 : 내 의심을 자세히 설명하기 위해 코드 샘플 추가
위에서 언급 한 문서에서 인용 하면 다음 코드 onPause()
의 AlertDialog
(또는 Dialog
)가 표시 될 때 내 활동의 메서드가 호출 되어야 합니까? 대화 상자가 표시 될 때 "onPause called"로그 항목이 표시되어야합니까?
그러나 나는 그런 일이 일어나지 않는다. Android 수명주기를 올바르게 이해했다면 그것도 마찬가지입니다! 그렇다면 문서가 가리키는 것은 무엇입니까?
public class LifeCycleTestActivity extends Activity {
private static final String TAG = "LifeCycleTest";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
AlertDialog dialog = new AlertDialog.Builder(LifeCycleTestActivity.this).create();
dialog.setMessage("You Clicked on the button");
dialog.setTitle("Dialog!");
dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.setCancelable(true);
dialog.show();
/*
Dialog dialog = new Dialog(LifeCycleTestActivity.this);
dialog.setTitle("Dialog!");
dialog.setCancelable(true);
dialog.show();
*/
}
});
}
@Override
protected void onPause() {
Log.d(TAG, "onPause() called");
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
}
onPause()
활동이 더 이상 활동 스택의 맨 위에 있지 않을 때 호출됩니다. 대화 자체는 활동이 아니므로 스택 맨 위에있는 현재 활동을 대체하지 않으므로 아무것도 일시 중지하지 않습니다.
그러나 대화 상자 (소문자)는 Dialog 클래스에서 구현할 필요가 없습니다. 예를 들어, 테마가 대화의 테마로 설정된 활동으로 구현하는 것은 드문 일이 아닙니다. 이 경우 대화 상자를 활동으로 표시하면 새 활동이 스택 맨 위에있게되고 이전에 있었던 활동이 일시 중지됩니다.
나는 AlertDialog
당신이 언급 한 것을 포함하여 대화 상자로 꽤 많은 코드를 작성했으며 대화 onPause()
상자가 나타날 때 활동에서 호출 되는지 확인하려고 시도 했지만 지금까지 내 결론 은 활동이 단순히 계속 실행되며 onPause()
호출되지 않습니다 .
그것이 도움이되는지 확실하지 않지만 적어도 당신은 당신이 경험하는 것을 경험하는 다른 사람들이 있다는 것을 알고 있습니다 :-)
Its wrong that activity remains no longer at top of activity stack in onPause phase.
Condition an activity to be onPause state -
- Activity partially visible e.g. dialog on activity.
The Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager.
e.g Home button pressed causes activity to go in onPause(). Still at top of stack.
In fig 1. Activity3 will be destroyed and removed from top stack
In fig 2. Now Task A goes to background but Activty X still on top of stack . If you override onPause() method int this state
Figure 1. A representation of how each new activity in a task adds an item to the back stack. When the user presses the Back button, the current activity is destroyed and the previous activity resumes.
Figure 2. Two tasks: Task B receives user interaction in the foreground, while Task A is in the background, waiting to be resumed.
I think I remember reading in an earlier version of the Android Lifecycle that onPause was called when none of the activity is on display. i.e. if a bit of your activity is still visible under a popup, onPause will not be called.
Maybe some other experts can vouch for this behavior?
In my slightly weird experience onResume
gets called with dialog.setCanceledOnTouchOutside(true);
but onPause
never gets called.
That being said, I think the documentation might focus on system dialogs (e.g. low on battery).
@hackbot
onPause() is called when your activity is no longer at the top of the activity >stack. A Dialog by itself is not an Activity, so will not replace the current >Activity at the top of the stack, so will not cause anything to pause.
everything depends on implementation...
what is a Dialog ? is a Window added to Display by WindowManager/// so the window when it shows is on top of everything .... (Z order)
what is activity... is "thing" that also creates its window....
when a dialog is shown or it's window comes visible on top of an existing activity, then it overrides partial the activity window so existing activity will move to partially invisible state and you will get call to onPause() from ActivityThread.
but to be sure we also need to consider here a one think...
the state of window if is a standalone window shown on top or it is a child window and a parent of it is a activity window....
so when we know
- the Window.LayoutParams (FLAGS) we use to add
- and what IBinder is used for the Window to show
we will khow how the activity will behave when windows are shown each over other .. as each winndow has a callbacks they are used by activity or dialog to manage their states...
involved components:
android.os.IBinder
android.view.Window
android.view.Window.Callback
android.view.WindowManager
android.view.WindowManager.LayoutParams
android.view.Display
btw:
if you want to know the windows on screen [ applicable only for the process you own - as window belongs to process and those are Sandboxed - each processs is a separate JVM strictly saying "ART" ] you can use a replection see :
- android.view.WindowManagerImpl
- android.view.WindowManagerGlobal
onPause()
is called every Time when an Activity goes background and Dialog
or other Activity
comes foreGround. This is done to give first priority to something with which the user is interacting. e.g: assume you are in homescreen (which in turn is an activity) of an application, the homescreen is said to be in foreground
. and when you go to next screen by pressing some button or a dialog appears the next screen/Activity/Dialog
comes to foreGround
and homecreen goes to backGround, which just means homeScreen's onPause()
method got called.
'IT TIP' 카테고리의 다른 글
내 PHP 파일이 일반 텍스트로 표시되는 이유는 무엇입니까? (0) | 2020.10.25 |
---|---|
콘솔 응용 프로그램을 닫는 명령? (0) | 2020.10.25 |
파이썬에서 주어진 문자열의 가능한 모든 순열 찾기 (0) | 2020.10.25 |
독 메뉴에서 xcode 최근 프로젝트 제거 (0) | 2020.10.25 |
node.js : '요청'모듈을 찾을 수 없습니다. (0) | 2020.10.25 |